Skip to content

Commit 896299b

Browse files
committed
optional: use singleton instead of inline variable for c++14
1 parent 40f8f79 commit 896299b

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

include/proxsuite/helpers/optional.hpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,23 @@ using optional = std::optional<T>;
2121
using nullopt_t = std::nullopt_t;
2222
inline constexpr nullopt_t nullopt = std::nullopt;
2323
#else
24+
namespace detail {
25+
// Source boost: https://www.boost.org/doc/libs/1_74_0/boost/none.hpp
26+
// the trick here is to make instance defined once as a global but in a header
27+
// file
28+
template<typename T>
29+
struct nullopt_instance
30+
{
31+
static const T instance;
32+
};
33+
template<typename T>
34+
const T nullopt_instance<T>::instance =
35+
T(tl::nullopt); // global, but because 'tis a template, no cpp file required
36+
} // namespace detail
2437
template<class T>
2538
using optional = tl::optional<T>;
2639
using nullopt_t = tl::nullopt_t;
27-
inline constexpr nullopt_t nullopt = tl::nullopt;
40+
constexpr nullopt_t nullopt = detail::nullopt_instance<tl::nullopt_t>::instance;
2841
#endif
2942
} // namespace proxsuite
3043

0 commit comments

Comments
 (0)