Skip to content

Commit

Permalink
fix inconsistent choice of init list constructor
Browse files Browse the repository at this point in the history
On some compilers value{x} invokes initializer_list constructor, on
others it is equivalent to value(x). This is very problematic, but this
isn't something we can fix. On the other hand, we CAN make init list
construction to be equivalent to value(x), if the size of init list is
one. This commit does exactly that.
  • Loading branch information
grisumbras committed Jul 21, 2023
1 parent 0a7860f commit c854e44
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
10 changes: 9 additions & 1 deletion include/boost/json/impl/value.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -231,13 +231,21 @@ value(
storage_ptr sp)
{
if(value_ref::maybe_object(init))
{
::new(&obj_) object(
value_ref::make_object(
init, std::move(sp)));
else
}
else if( init.size() != 1 )
{
::new(&arr_) array(
value_ref::make_array(
init, std::move(sp)));
}
else
{
::new(this) value( init.begin()->make_value( std::move(sp) ) );
}
}

//----------------------------------------------------------
Expand Down
5 changes: 5 additions & 0 deletions test/value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2131,6 +2131,11 @@ class value_test
void
testInitList()
{
{
value jv{0};
BOOST_TEST( jv == 0 );
}

check_array(value{0,0,0}, 0, 0, 0);
check_array(value{false,false,false}, false, false, false);
check_array(value{false,2,false}, false, 2, false);
Expand Down

0 comments on commit c854e44

Please sign in to comment.