mirror of
https://github.com/boostorg/container_hash.git
synced 2026-08-03 20:24:15 +02:00
Add a test for valueless std::variant values
This commit is contained in:
@@ -135,3 +135,5 @@ run hash_tuple_like_test2.cpp
|
||||
|
||||
run is_range_test4.cpp ;
|
||||
run hash_container_test2.cpp ;
|
||||
|
||||
run hash_variant_test2.cpp ;
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
// Copyright 2026 Peter Dimov
|
||||
// Distributed under the Boost Software License, Version 1.0.
|
||||
// https://www.boost.org/LICENSE_1_0.txt
|
||||
|
||||
#include <boost/container_hash/hash.hpp>
|
||||
#include <boost/config.hpp>
|
||||
#include <boost/config/pragma_message.hpp>
|
||||
|
||||
#if defined(BOOST_NO_CXX17_HDR_VARIANT)
|
||||
|
||||
BOOST_PRAGMA_MESSAGE( "Skipping test because BOOST_NO_CXX17_HDR_VARIANT is defined" )
|
||||
int main() {}
|
||||
|
||||
#else
|
||||
|
||||
#include <boost/core/lightweight_test.hpp>
|
||||
#include <variant>
|
||||
|
||||
struct X
|
||||
{
|
||||
operator int() const { throw 5; }
|
||||
};
|
||||
|
||||
std::variant<int, float> make_valueless_variant()
|
||||
{
|
||||
std::variant<int, float> v;
|
||||
|
||||
try
|
||||
{
|
||||
v.emplace<int>( X() );
|
||||
}
|
||||
catch( int )
|
||||
{
|
||||
}
|
||||
|
||||
BOOST_TEST( v.valueless_by_exception() );
|
||||
|
||||
return v;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
std::variant<int, float> v1, v2 = make_valueless_variant();
|
||||
|
||||
BOOST_TEST_NE( (boost::hash<std::variant<int, float>>()( v1 )), (boost::hash<std::variant<int, float>>()( v2 )) );
|
||||
|
||||
return boost::report_errors();
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user