Advertisement
Guest User

Untitled

a guest
Dec 12th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.10 KB | None | 0 0
  1. #include <memory>
  2. #include <vector>
  3. #include <optional>
  4. #include <variant>
  5.  
  6. using namespace std;
  7. class Value {
  8. public:
  9.   enum class Kind { Boolean = 0, Integer, Float, Double, String };
  10.   template <typename T>
  11.   struct TypeInfo;
  12.  
  13.   template <typename T>
  14.   Value(T&& value)
  15.       : value_{std::forward<T>(value)},
  16.         is_constexpr_{false}, type_{TypeInfo<std::decay_t<T>>::Type} {}  
  17.  
  18.    Value(Value&& other)
  19.       : value_{std::move(other.value_)}, is_constexpr_{other.is_constexpr_}, type_{other.type_} {}
  20.  
  21.    Value(Value& other)
  22.       : value_{other.value_}, is_constexpr_{other.is_constexpr_}, type_{other.type_} {}
  23.  
  24. private:
  25.   std::optional<std::variant<bool, int, float, double, std::string>> value_;
  26.   bool is_constexpr_;
  27.   Kind type_;
  28. };
  29.  
  30. template <>
  31. struct Value::TypeInfo<double> {
  32.   static constexpr Kind Type = Kind::Double;
  33. };
  34.  
  35. void foo(const std::vector<Value>& in) {
  36.     std::vector<Value> temp = in;   //matches Value(T&& in)!
  37. }
  38.  
  39. int main() {
  40.     Value val1(1.);
  41.     Value val2(2.);
  42.     Value val3(2.);
  43.  
  44.     std::vector<Value> vec1({val1, val2, val3});
  45.     foo(vec1);
  46.  
  47.     return 0;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement