From c056f3dfc9362a3e3642046b00b0d9c777fab281 Mon Sep 17 00:00:00 2001 From: Braden Ganetsky Date: Tue, 18 Jun 2024 21:49:48 -0500 Subject: [PATCH 1/4] Write failing test in MSVC for deriving from empty_value --- test/empty_value_test.cpp | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/test/empty_value_test.cpp b/test/empty_value_test.cpp index b7cf796..fc03729 100644 --- a/test/empty_value_test.cpp +++ b/test/empty_value_test.cpp @@ -63,6 +63,37 @@ 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() +{ + 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(); From e22bd49fa717f065491058fb4b50a6ffbee4532f Mon Sep 17 00:00:00 2001 From: Braden Ganetsky Date: Thu, 11 Jul 2024 17:02:21 -0500 Subject: [PATCH 2/4] Fix MSVC issue with deriving from empty_value --- include/boost/core/empty_value.hpp | 54 +++++++++++++++++++++++++++--- 1 file changed, 49 insertions(+), 5 deletions(-) diff --git a/include/boost/core/empty_value.hpp b/include/boost/core/empty_value.hpp index d8ffa30..ee86810 100644 --- a/include/boost/core/empty_value.hpp +++ b/include/boost/core/empty_value.hpp @@ -95,9 +95,53 @@ private: }; #if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) +#if defined(BOOST_MSVC) +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 +152,26 @@ public: #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 { From d01b4e94cc72abaae859d1fe739ca6b81e2cfa04 Mon Sep 17 00:00:00 2001 From: Braden Ganetsky Date: Mon, 24 Jun 2024 13:15:05 -0500 Subject: [PATCH 3/4] Write test for empty_value private inheritance --- test/Jamfile.v2 | 1 + test/empty_value_compile_fail_casting.cpp | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+) create mode 100644 test/empty_value_compile_fail_casting.cpp diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index 5df9242..e9a1d07 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 0000000..e4cac42 --- /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() +{ +} From acbeaae1815f1db8e7213f21144d66de69d0182c Mon Sep 17 00:00:00 2001 From: Braden Ganetsky Date: Thu, 27 Jun 2024 12:43:01 -0500 Subject: [PATCH 4/4] Add link to Visual Studio bug report --- include/boost/core/empty_value.hpp | 2 ++ test/empty_value_test.cpp | 3 +++ 2 files changed, 5 insertions(+) diff --git a/include/boost/core/empty_value.hpp b/include/boost/core/empty_value.hpp index ee86810..bb95841 100644 --- a/include/boost/core/empty_value.hpp +++ b/include/boost/core/empty_value.hpp @@ -96,6 +96,8 @@ private: #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 diff --git a/test/empty_value_test.cpp b/test/empty_value_test.cpp index fc03729..44dba4c 100644 --- a/test/empty_value_test.cpp +++ b/test/empty_value_test.cpp @@ -79,6 +79,9 @@ struct outer { 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);