Skip to content

Commit

Permalink
Merge pull request #175 from k3DW/feature/empty-value-inherit-public
Browse files Browse the repository at this point in the history
Fix `empty_value` MSVC bug
glenfe authored Jul 12, 2024
2 parents 3f36d50 + acbeaae commit 83a3a51
Showing 4 changed files with 106 additions and 5 deletions.
56 changes: 51 additions & 5 deletions include/boost/core/empty_value.hpp
Original file line number Diff line number Diff line change
@@ -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 T>
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<class U, class... Args>
BOOST_CONSTEXPR empty_value_base(U&& value, Args&&... args)
: T(std::forward<U>(value), std::forward<Args>(args)...) { }
#else
template<class U>
BOOST_CONSTEXPR empty_value_base(U&& value)
: T(std::forward<U>(value)) { }
#endif
#else
template<class U>
BOOST_CONSTEXPR empty_value_base(const U& value)
: T(value) { }

template<class U>
BOOST_CONSTEXPR empty_value_base(U& value)
: T(value) { }
#endif
};

} /* detail */
#endif

template<class T, unsigned N>
class empty_value<T, N, true>
#if defined(BOOST_MSVC)
: detail::empty_value_base<T> {
typedef detail::empty_value_base<T> base;
#else
: T {
typedef T base;
#endif

public:
typedef T type;

@@ -108,26 +154,26 @@ class empty_value<T, N, true>
#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<class U, class... Args>
BOOST_CONSTEXPR empty_value(boost::empty_init_t, U&& value, Args&&... args)
: T(std::forward<U>(value), std::forward<Args>(args)...) { }
: base(std::forward<U>(value), std::forward<Args>(args)...) { }
#else
template<class U>
BOOST_CONSTEXPR empty_value(boost::empty_init_t, U&& value)
: T(std::forward<U>(value)) { }
: base(std::forward<U>(value)) { }
#endif
#else
template<class U>
BOOST_CONSTEXPR empty_value(boost::empty_init_t, const U& value)
: T(value) { }
: base(value) { }

template<class U>
BOOST_CONSTEXPR empty_value(boost::empty_init_t, U& value)
: T(value) { }
: base(value) { }
#endif

BOOST_CONSTEXPR const T& get() const BOOST_NOEXCEPT {
1 change: 1 addition & 0 deletions test/Jamfile.v2
Original file line number Diff line number Diff line change
@@ -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 ;
20 changes: 20 additions & 0 deletions test/empty_value_compile_fail_casting.cpp
Original file line number Diff line number Diff line change
@@ -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 <boost/core/empty_value.hpp>

struct empty {};

// This test ensures private inheritance of `boost::empty_value<T>` for empty `T`.
// With public inheritance, `boost::empty_value<empty>*` could cast to `empty*`.
void test_empty_not_convertible_to_base()
{
const boost::empty_value<empty> x(boost::empty_init);
const empty* x2 = static_cast<const empty*>(&x);
(void)x2;
}

int main()
{
}
34 changes: 34 additions & 0 deletions test/empty_value_test.cpp
Original file line number Diff line number Diff line change
@@ -63,6 +63,40 @@ void test_type()
BOOST_TEST(v2.get().value() == 6);
}

template <class T>
struct derived : boost::empty_value<T> {
typedef typename boost::empty_value<T>::type type;
derived(boost::empty_init_t e) : boost::empty_value<T>(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<outer> x1(boost::empty_init);
const boost::empty_value<outer::inner_empty> x2(boost::empty_init);
const boost::empty_value<outer::inner_non_empty> x3(boost::empty_init);
const derived<outer> x4(boost::empty_init);
const derived<outer::inner_empty> x5(boost::empty_init);
const derived<outer::inner_non_empty> x6(boost::empty_init);

(void)x1;
(void)x2;
(void)x3;
(void)x4;
(void)x5;
(void)x6;
}

int main()
{
test_int();

0 comments on commit 83a3a51

Please sign in to comment.