| 1 |
// Copyright (C) 2016-2025 Free Software Foundation, Inc. |
| 2 |
// |
| 3 |
// This file is part of the GNU ISO C++ Library. This library is free |
| 4 |
// software; you can redistribute it and/or modify it under the |
| 5 |
// terms of the GNU General Public License as published by the |
| 6 |
// Free Software Foundation; either version 3, or (at your option) |
| 7 |
// any later version. |
| 8 |
|
| 9 |
// This library is distributed in the hope that it will be useful, |
| 10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
// GNU General Public License for more details. |
| 13 |
|
| 14 |
// You should have received a copy of the GNU General Public License along |
| 15 |
// with this library; see the file COPYING3. If not see |
| 16 |
// <http://www.gnu.org/licenses/>. |
| 17 |
|
| 18 |
// { dg-options "-O0" } |
| 19 |
// { dg-do run { target c++11 } } |
| 20 |
|
| 21 |
#include <string> |
| 22 |
#include <testsuite_hooks.h> |
| 23 |
|
| 24 |
struct TestBaseObjCtor : std::string |
| 25 |
{ |
| 26 |
template<typename... Args> |
| 27 |
TestBaseObjCtor(Args&&... args) |
| 28 |
: std::string(std::forward<Args>(args)...) |
| 29 |
{ } |
| 30 |
}; |
| 31 |
|
| 32 |
template<typename... Args> |
| 33 |
std::size_t |
| 34 |
construct(Args&&... args) |
| 35 |
{ |
| 36 |
// Use static_cast<Args> to produce either an lvalue or prvalue, |
| 37 |
// so args... not left in moved-from state and can be reused below: |
| 38 |
TestBaseObjCtor as_base_obj( static_cast<Args>(args)... ); |
| 39 |
|
| 40 |
std::string as_complete_obj( std::forward<Args>(args)... ); |
| 41 |
|
| 42 |
return as_complete_obj.length(); |
| 43 |
} |
| 44 |
|
| 45 |
void |
| 46 |
test01() |
| 47 |
{ |
| 48 |
using string = std::string; |
| 49 |
using list = std::initializer_list<string::value_type>; |
| 50 |
|
| 51 |
const std::string lvalue = "lvalue"; |
| 52 |
std::allocator<char> alloc; |
| 53 |
|
| 54 |
// test all valid combinations of arguments: |
| 55 |
VERIFY( construct( ) == 0 ); |
| 56 |
VERIFY( construct( alloc ) == 0 ); |
| 57 |
VERIFY( construct( lvalue ) == 6 ); |
| 58 |
VERIFY( construct( string{"rvalue"} ) == 6 ); |
| 59 |
VERIFY( construct( lvalue, 2 ) == 4 ); |
| 60 |
VERIFY( construct( lvalue, 2, alloc ) == 4 ); |
| 61 |
VERIFY( construct( lvalue, 2, 3 ) == 3 ); |
| 62 |
VERIFY( construct( lvalue, 2, 3, alloc ) == 3 ); |
| 63 |
VERIFY( construct( "C string", 4 ) == 4 ); |
| 64 |
VERIFY( construct( "C string", 4, alloc ) == 4 ); |
| 65 |
VERIFY( construct( "C string" ) == 8 ); |
| 66 |
VERIFY( construct( "C string and alloc", alloc ) == 18 ); |
| 67 |
VERIFY( construct( 5, ' ' ) == 5 ); |
| 68 |
VERIFY( construct( 5, ' ', alloc ) == 5 ); |
| 69 |
VERIFY( construct( lvalue.begin(), lvalue.end() ) == 6 ); |
| 70 |
VERIFY( construct( lvalue.begin(), lvalue.end(), alloc ) == 6 ); |
| 71 |
VERIFY( construct( list{ 'l' , 'i' , 's', 't' } ) == 4 ); |
| 72 |
VERIFY( construct( list{ 'l', 'i', 's', 't' }, alloc ) == 4 ); |
| 73 |
VERIFY( construct( lvalue, alloc ) == 6 ); |
| 74 |
VERIFY( construct( string{"rvalue"}, alloc ) == 6 ); |
| 75 |
} |
| 76 |
|
| 77 |
int |
| 78 |
main() |
| 79 |
{ |
| 80 |
test01(); |
| 81 |
} |