Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <memory>
- #include <vector>
- #include <optional>
- #include <variant>
- using namespace std;
- class Value {
- public:
- enum class Kind { Boolean = 0, Integer, Float, Double, String };
- template <typename T>
- struct TypeInfo;
- template <typename T>
- Value(T&& value)
- : value_{std::forward<T>(value)},
- is_constexpr_{false}, type_{TypeInfo<std::decay_t<T>>::Type} {}
- Value(Value&& other)
- : value_{std::move(other.value_)}, is_constexpr_{other.is_constexpr_}, type_{other.type_} {}
- Value(Value& other)
- : value_{other.value_}, is_constexpr_{other.is_constexpr_}, type_{other.type_} {}
- private:
- std::optional<std::variant<bool, int, float, double, std::string>> value_;
- bool is_constexpr_;
- Kind type_;
- };
- template <>
- struct Value::TypeInfo<double> {
- static constexpr Kind Type = Kind::Double;
- };
- void foo(const std::vector<Value>& in) {
- std::vector<Value> temp = in; //matches Value(T&& in)!
- }
- int main() {
- Value val1(1.);
- Value val2(2.);
- Value val3(2.);
- std::vector<Value> vec1({val1, val2, val3});
- foo(vec1);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement