mirror of
https://github.com/boostorg/core.git
synced 2025-05-10 07:13:54 +00:00
Add constexpr support to empty_value
This commit is contained in:
parent
4162dbed57
commit
e3745b2072
@ -56,37 +56,37 @@ public:
|
||||
#if !defined(BOOST_NO_CXX11_DEFAULTED_FUNCTIONS)
|
||||
empty_value() = default;
|
||||
#else
|
||||
empty_value() { }
|
||||
BOOST_CONSTEXPR empty_value() { }
|
||||
#endif
|
||||
|
||||
empty_value(boost::empty_init_t)
|
||||
BOOST_CONSTEXPR empty_value(boost::empty_init_t)
|
||||
: value_() { }
|
||||
|
||||
#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
|
||||
#if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
|
||||
template<class U, class... Args>
|
||||
empty_value(boost::empty_init_t, U&& value, Args&&... args)
|
||||
BOOST_CONSTEXPR empty_value(boost::empty_init_t, U&& value, Args&&... args)
|
||||
: value_(std::forward<U>(value), std::forward<Args>(args)...) { }
|
||||
#else
|
||||
template<class U>
|
||||
empty_value(boost::empty_init_t, U&& value)
|
||||
BOOST_CONSTEXPR empty_value(boost::empty_init_t, U&& value)
|
||||
: value_(std::forward<U>(value)) { }
|
||||
#endif
|
||||
#else
|
||||
template<class U>
|
||||
empty_value(boost::empty_init_t, const U& value)
|
||||
BOOST_CONSTEXPR empty_value(boost::empty_init_t, const U& value)
|
||||
: value_(value) { }
|
||||
|
||||
template<class U>
|
||||
empty_value(boost::empty_init_t, U& value)
|
||||
BOOST_CONSTEXPR empty_value(boost::empty_init_t, U& value)
|
||||
: value_(value) { }
|
||||
#endif
|
||||
|
||||
const T& get() const BOOST_NOEXCEPT {
|
||||
BOOST_CONSTEXPR const T& get() const BOOST_NOEXCEPT {
|
||||
return value_;
|
||||
}
|
||||
|
||||
T& get() BOOST_NOEXCEPT {
|
||||
BOOST_CXX14_CONSTEXPR T& get() BOOST_NOEXCEPT {
|
||||
return value_;
|
||||
}
|
||||
|
||||
@ -104,37 +104,37 @@ public:
|
||||
#if !defined(BOOST_NO_CXX11_DEFAULTED_FUNCTIONS)
|
||||
empty_value() = default;
|
||||
#else
|
||||
empty_value() { }
|
||||
BOOST_CONSTEXPR empty_value() { }
|
||||
#endif
|
||||
|
||||
empty_value(boost::empty_init_t)
|
||||
BOOST_CONSTEXPR empty_value(boost::empty_init_t)
|
||||
: T() { }
|
||||
|
||||
#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
|
||||
#if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
|
||||
template<class U, class... Args>
|
||||
empty_value(boost::empty_init_t, U&& value, Args&&... args)
|
||||
BOOST_CONSTEXPR empty_value(boost::empty_init_t, U&& value, Args&&... args)
|
||||
: T(std::forward<U>(value), std::forward<Args>(args)...) { }
|
||||
#else
|
||||
template<class U>
|
||||
empty_value(boost::empty_init_t, U&& value)
|
||||
BOOST_CONSTEXPR empty_value(boost::empty_init_t, U&& value)
|
||||
: T(std::forward<U>(value)) { }
|
||||
#endif
|
||||
#else
|
||||
template<class U>
|
||||
empty_value(boost::empty_init_t, const U& value)
|
||||
BOOST_CONSTEXPR empty_value(boost::empty_init_t, const U& value)
|
||||
: T(value) { }
|
||||
|
||||
template<class U>
|
||||
empty_value(boost::empty_init_t, U& value)
|
||||
BOOST_CONSTEXPR empty_value(boost::empty_init_t, U& value)
|
||||
: T(value) { }
|
||||
#endif
|
||||
|
||||
const T& get() const BOOST_NOEXCEPT {
|
||||
BOOST_CONSTEXPR const T& get() const BOOST_NOEXCEPT {
|
||||
return *this;
|
||||
}
|
||||
|
||||
T& get() BOOST_NOEXCEPT {
|
||||
BOOST_CXX14_CONSTEXPR T& get() BOOST_NOEXCEPT {
|
||||
return *this;
|
||||
}
|
||||
};
|
||||
|
@ -203,6 +203,7 @@ compile first_scalar_constexpr_test.cpp ;
|
||||
run empty_value_test.cpp ;
|
||||
run empty_value_size_test.cpp ;
|
||||
run empty_value_final_test.cpp ;
|
||||
run empty_value_constexpr_test.cpp ;
|
||||
|
||||
run quick_exit_test.cpp ;
|
||||
run-fail quick_exit_fail.cpp ;
|
||||
|
65
test/empty_value_constexpr_test.cpp
Normal file
65
test/empty_value_constexpr_test.cpp
Normal file
@ -0,0 +1,65 @@
|
||||
/*
|
||||
Copyright 2022 Glen Joseph Fernandes
|
||||
(glenjofe@gmail.com)
|
||||
|
||||
Distributed under the Boost Software License, Version 1.0.
|
||||
(http://www.boost.org/LICENSE_1_0.txt)
|
||||
*/
|
||||
#include <boost/config.hpp>
|
||||
#if !defined(BOOST_NO_CXX11_CONSTEXPR)
|
||||
#include <boost/core/empty_value.hpp>
|
||||
#include <boost/core/lightweight_test.hpp>
|
||||
|
||||
struct empty {
|
||||
constexpr int value() const {
|
||||
return 1;
|
||||
}
|
||||
};
|
||||
|
||||
class type {
|
||||
public:
|
||||
explicit constexpr type(int count)
|
||||
: value_(count) { }
|
||||
|
||||
constexpr int value() const {
|
||||
return value_;
|
||||
}
|
||||
|
||||
private:
|
||||
int value_;
|
||||
};
|
||||
|
||||
void test_int()
|
||||
{
|
||||
constexpr boost::empty_value<int> v(boost::empty_init_t(), 4);
|
||||
constexpr int c = v.get();
|
||||
BOOST_TEST_EQ(c, 4);
|
||||
}
|
||||
|
||||
void test_empty()
|
||||
{
|
||||
constexpr boost::empty_value<empty> v = boost::empty_init_t();
|
||||
constexpr int c = v.get().value();
|
||||
BOOST_TEST_EQ(c, 1);
|
||||
}
|
||||
|
||||
void test_type()
|
||||
{
|
||||
constexpr boost::empty_value<type> v(boost::empty_init_t(), 2);
|
||||
constexpr int c = v.get().value();
|
||||
BOOST_TEST_EQ(c, 2);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
test_int();
|
||||
test_empty();
|
||||
test_type();
|
||||
return boost::report_errors();
|
||||
}
|
||||
#else
|
||||
int main()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
#endif
|
Loading…
x
Reference in New Issue
Block a user