diff --git a/include/boost/variant2/variant.hpp b/include/boost/variant2/variant.hpp index 3eb5342..f6896f0 100644 --- a/include/boost/variant2/variant.hpp +++ b/include/boost/variant2/variant.hpp @@ -2125,7 +2125,7 @@ template struct visit_L1 F&& f; V1&& v1; - template auto operator()( I ) const -> Vret + template constexpr auto operator()( I ) const -> Vret { return std::forward(f)( unsafe_get( std::forward(v1) ) ); } diff --git a/test/Jamfile b/test/Jamfile index 17cc75d..2bed75d 100644 --- a/test/Jamfile +++ b/test/Jamfile @@ -139,3 +139,5 @@ run variant_json_value_to.cpp : : : $(JSON) ; compile variant_uses_double_storage.cpp ; run variant_derived_construct2.cpp ; + +compile variant_visit_cx.cpp : [ requires cxx14_constexpr ] ; diff --git a/test/variant_visit_cx.cpp b/test/variant_visit_cx.cpp new file mode 100644 index 0000000..4646242 --- /dev/null +++ b/test/variant_visit_cx.cpp @@ -0,0 +1,51 @@ +// Copyright 2017, 2024 Peter Dimov +// Distributed under the Boost Software License, Version 1.0. +// http://www.boost.org/LICENSE_1_0.txt + +#include +using namespace boost::variant2; + +#if !defined(BOOST_MP11_HAS_CXX14_CONSTEXPR) + +#include + +BOOST_PRAGMA_MESSAGE("Skipping constexpr visit test because BOOST_MP11_HAS_CXX14_CONSTEXPR is not defined") + +int main() {} + +#else + +struct X +{ + constexpr operator int() const { return 2; } +}; + +struct F +{ + constexpr int operator()( int x ) const + { + return x; + } +}; + +#define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__) + +int main() +{ + { + constexpr variant v( 1 ); + STATIC_ASSERT( visit( F(), v ) == 1 ); + } + + { + constexpr variant v( 'a' ); + STATIC_ASSERT( visit( F(), v ) == 'a' ); + } + + { + constexpr variant v( X{} ); + STATIC_ASSERT( visit( F(), v ) == 2 ); + } +} + +#endif