diff --git a/include/boost/core/empty_value.hpp b/include/boost/core/empty_value.hpp index d8ffa30b..bb958419 100644 --- a/include/boost/core/empty_value.hpp +++ b/include/boost/core/empty_value.hpp @@ -95,9 +95,55 @@ class empty_value { }; #if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) +#if defined(BOOST_MSVC) +// This is a workaround to an MSVC bug when T is a nested class. +// See https://developercommunity.visualstudio.com/t/Compiler-bug:-Incorrect-C2247-and-C2248/10690025 +namespace detail { + +template +class empty_value_base + : public T { +public: +#if !defined(BOOST_NO_CXX11_DEFAULTED_FUNCTIONS) + empty_value_base() = default; +#else + BOOST_CONSTEXPR empty_value_base() { } +#endif + +#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) +#if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) + template + BOOST_CONSTEXPR empty_value_base(U&& value, Args&&... args) + : T(std::forward(value), std::forward(args)...) { } +#else + template + BOOST_CONSTEXPR empty_value_base(U&& value) + : T(std::forward(value)) { } +#endif +#else + template + BOOST_CONSTEXPR empty_value_base(const U& value) + : T(value) { } + + template + BOOST_CONSTEXPR empty_value_base(U& value) + : T(value) { } +#endif +}; + +} /* detail */ +#endif + template class empty_value +#if defined(BOOST_MSVC) + : detail::empty_value_base { + typedef detail::empty_value_base base; +#else : T { + typedef T base; +#endif + public: typedef T type; @@ -108,26 +154,26 @@ class empty_value #endif BOOST_CONSTEXPR empty_value(boost::empty_init_t) - : T() { } + : base() { } #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) template BOOST_CONSTEXPR empty_value(boost::empty_init_t, U&& value, Args&&... args) - : T(std::forward(value), std::forward(args)...) { } + : base(std::forward(value), std::forward(args)...) { } #else template BOOST_CONSTEXPR empty_value(boost::empty_init_t, U&& value) - : T(std::forward(value)) { } + : base(std::forward(value)) { } #endif #else template BOOST_CONSTEXPR empty_value(boost::empty_init_t, const U& value) - : T(value) { } + : base(value) { } template BOOST_CONSTEXPR empty_value(boost::empty_init_t, U& value) - : T(value) { } + : base(value) { } #endif BOOST_CONSTEXPR const T& get() const BOOST_NOEXCEPT { diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index 5df92426..e9a1d07d 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -226,6 +226,7 @@ run empty_value_test.cpp ; run empty_value_size_test.cpp ; run empty_value_final_test.cpp ; run empty_value_constexpr_test.cpp ; +compile-fail empty_value_compile_fail_casting.cpp ; run quick_exit_test.cpp ; run-fail quick_exit_fail.cpp ; diff --git a/test/empty_value_compile_fail_casting.cpp b/test/empty_value_compile_fail_casting.cpp new file mode 100644 index 00000000..e4cac42d --- /dev/null +++ b/test/empty_value_compile_fail_casting.cpp @@ -0,0 +1,20 @@ +// Copyright 2024 Braden Ganetsky +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt + +#include + +struct empty {}; + +// This test ensures private inheritance of `boost::empty_value` for empty `T`. +// With public inheritance, `boost::empty_value*` could cast to `empty*`. +void test_empty_not_convertible_to_base() +{ + const boost::empty_value x(boost::empty_init); + const empty* x2 = static_cast(&x); + (void)x2; +} + +int main() +{ +} diff --git a/test/empty_value_test.cpp b/test/empty_value_test.cpp index b7cf7961..44dba4c4 100644 --- a/test/empty_value_test.cpp +++ b/test/empty_value_test.cpp @@ -63,6 +63,40 @@ void test_type() BOOST_TEST(v2.get().value() == 6); } +template +struct derived : boost::empty_value { + typedef typename boost::empty_value::type type; + derived(boost::empty_init_t e) : boost::empty_value(e) {} +}; + +struct outer { + struct inner_empty {}; + struct inner_non_empty { + inner_non_empty() : value() {} + int value; + }; +}; + +void test_derived_compile() +{ + // This is testing the workaround to an MSVC bug when T is a nested class. + // See https://developercommunity.visualstudio.com/t/Compiler-bug:-Incorrect-C2247-and-C2248/10690025 + + const boost::empty_value x1(boost::empty_init); + const boost::empty_value x2(boost::empty_init); + const boost::empty_value x3(boost::empty_init); + const derived x4(boost::empty_init); + const derived x5(boost::empty_init); + const derived x6(boost::empty_init); + + (void)x1; + (void)x2; + (void)x3; + (void)x4; + (void)x5; + (void)x6; +} + int main() { test_int();