Closed KarimLUCCIN closed 5 years ago
Thanks @KarimLUCCIN ,
Good catch.
max_index()
means _max_alternativeindex().
Its implemention is wrong, and it is used erroneously and superfluously in this and this check for invalid access.
The bad access can be revealed more directly via get<type>(var)
:
using nonstd::get;
using variant_t = nonstd::variant<N<0>,N<1>,N<2>,N<3>, std::int32_t>;
try
{
(void) get<std::int32_t>( variant_t( 13 ) ) );
}
catch( std::exception const & e )
{
std::cout << "Error: '" << e.what() << "'\n";
}
yielding: Error: 'bad variant access'
Thanks for the fix!
https://github.com/martinmoene/variant-lite/blob/8a5f0665f76160b6d038537b37771bd708d45de0/include/nonstd/variant.hpp#L1475
Was this line meant to be data_size, or instead the maximum number of types in the variant? It seems that the code below will cause a bad_access, because
int
is at index 4, which is also the data_size of the variant: