const_container/include/CompileOptional.h

47 lines
1.1 KiB
C
Raw Normal View History

2024-01-22 22:42:32 +01:00
//
// Created by Patrick Maschek on 22.01.2024.
//
#ifndef CONST_CONTAINER_COMPILEOPTIONAL_H_
#define CONST_CONTAINER_COMPILEOPTIONAL_H_
2024-01-22 23:28:47 +01:00
#include <type_traits>
#include <initializer_list>
2024-01-22 23:28:47 +01:00
2024-01-22 22:42:32 +01:00
namespace cc {
template<typename T, bool Cond = false>
2024-01-22 22:42:32 +01:00
class CompileOptional {};
2024-01-22 23:28:47 +01:00
template<typename T>
class CompileOptional<T, false> {
public:
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) {}
2024-01-22 23:28:47 +01:00
};
2024-01-22 22:42:32 +01:00
template<typename T>
class CompileOptional<T, true> {
public:
2024-01-22 23:28:47 +01:00
using defined = std::true_type;
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; }
2024-01-22 23:28:47 +01:00
private:
T _value;
2024-01-22 22:42:32 +01:00
};
2024-01-22 23:28:47 +01:00
2024-01-22 22:42:32 +01:00
}
2024-01-22 23:28:47 +01:00
#endif //CONST_CONTAINER_COMPILEOPTIONAL_H_