Skip to content

Commit 1b7e275

Browse files
fekirmartinmoene
authored andcommitted
Enhance in_place construction (#66)
The current implementation required ad least a move-constructor for in-place construction. This made it impossible to directly, create an optional of a type without copy and move operators. A workaround is using the variadic emplace function, but it requires to construct the object in two steps. std::optional does not require any copy or move operation for in-place construction
1 parent 52f2b61 commit 1b7e275

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

include/nonstd/optional.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1106,7 +1106,7 @@ class optional
11061106
>
11071107
optional_constexpr explicit optional( nonstd_lite_in_place_t(T), Args&&... args )
11081108
: has_value_( true )
1109-
, contained( T( std::forward<Args>(args)...) )
1109+
, contained( in_place, std::forward<Args>(args)... )
11101110
{}
11111111

11121112
// 7 (C++11) - in-place construct, initializer-list

test/optional.t.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,17 @@ CASE( "optional: Allows to default construct an empty optional with a non-defaul
223223
EXPECT( !ondcm );
224224
}
225225

226+
227+
CASE( "optional: construct an immovable object" )
228+
{
229+
#if optional_CPP11_OR_GREATER
230+
optional<NoDefaultCopyMove> ondcm(in_place, std::string("test"));
231+
EXPECT( ondcm.has_value() );
232+
#else
233+
EXPECT( !!"optional: in-place construction is not available (no C++11)" );
234+
#endif
235+
}
236+
226237
CASE( "optional: Allows to copy-construct from empty optional (2)" )
227238
{
228239
optional<int> a;
@@ -486,11 +497,7 @@ CASE( "optional: Allows to in-place copy-construct from value (C++11, 6)" )
486497

487498
EXPECT( a->first == 'a' );
488499
EXPECT( a->second.value == 7 );
489-
#if optional_USES_STD_OPTIONAL
490500
EXPECT( a->second.state == copy_constructed );
491-
#else
492-
EXPECT( a->second.state == move_constructed );
493-
#endif
494501
EXPECT( s.state != moved_from );
495502
#else
496503
EXPECT( !!"optional: in-place construction is not available (no C++11)" );
@@ -1254,11 +1261,7 @@ CASE( "make_optional: Allows to in-place copy-construct optional from arguments
12541261

12551262
EXPECT( a->first == 'a' );
12561263
EXPECT( a->second.value == 7 );
1257-
#if optional_USES_STD_OPTIONAL
12581264
EXPECT( a->second.state == copy_constructed );
1259-
#else
1260-
EXPECT( a->second.state == move_constructed );
1261-
#endif
12621265
EXPECT( s.state != moved_from );
12631266
#else
12641267
EXPECT( !!"optional: in-place construction is not available (no C++11)" );

0 commit comments

Comments
 (0)