added ctors and constexpr to CompileOptional

This commit is contained in:
cyborg1811m 2024-01-22 23:58:35 +01:00
parent e8fac3a44d
commit af5c9b2454
1 changed files with 18 additions and 3 deletions

View File

@ -6,24 +6,39 @@
#define CONST_CONTAINER_COMPILEOPTIONAL_H_ #define CONST_CONTAINER_COMPILEOPTIONAL_H_
#include <type_traits> #include <type_traits>
#include <initializer_list>
namespace cc { namespace cc {
template<typename, bool Cond = false> template<typename T, bool Cond = false>
class CompileOptional {}; class CompileOptional {};
template<typename T> template<typename T>
class CompileOptional<T, false> { class CompileOptional<T, false> {
public: public:
using defined = std::false_type; using defined = std::false_type;
using value_type = T;
constexpr CompileOptional() = default;
constexpr CompileOptional(const value_type& value) {}
template<typename ...Args>
constexpr CompileOptional(Args... args) {}
}; };
template<typename T> template<typename T>
class CompileOptional<T, true> { class CompileOptional<T, true> {
public: public:
using defined = std::true_type; using defined = std::true_type;
operator T&() { return _data; } using value_type = T;
constexpr CompileOptional() = default;
constexpr CompileOptional(const value_type& value) : _value(value) {}
constexpr CompileOptional(value_type&& value) : _value(value) {}
template<typename ...Args>
constexpr CompileOptional(Args... args) : _value( { std::forward<Args>(args)... } ) {}
constexpr operator T&() { return _value; }
private: private:
T _data; T _value;
}; };
} }