1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
9 template <typename _SearchType, typename _CurrentType, typename... _RestTypes>
10 struct VariantTypeSelector: VariantTypeSelector<_SearchType, _RestTypes...> {
13 template <typename _SearchType, typename... _RestTypes>
14 struct VariantTypeSelector<_SearchType, _SearchType, _RestTypes...> {
15 typedef _SearchType type;
18 template <typename... _Types>
21 typedef std::tuple_size<std::tuple<_Types...>> TypesTupleSize;
24 Variant(): valueType_(TypesTupleSize::value) {
27 Variant(const Variant& fromVariant):
28 valueType_(fromVariant.valueType_),
29 valueStorage_(fromVariant.valueStorage_) {
32 Variant(Variant&& fromVariant):
33 valueType_(std::move(fromVariant.valueType_)),
34 valueStorage_(std::move(fromVariant.valueStorage_)) {
35 fromVariant.valueType_ = TypesTupleSize::value;
40 // TODO call value destructor
44 Variant& operator=(const Variant& fromVariant) {
49 Variant& operator=(Variant&& fromVariant) {
54 // TODO use std::enable_if
55 template <typename _Type>
56 Variant(const _Type& value) {
59 new (&valueStorage_) _Type(value);
62 // TODO use std::enable_if
63 template <typename _Type>
64 Variant(_Type && value) {
67 new (&valueStorage_) typename std::remove_reference<_Type>::type(std::move(value));
70 template <typename _Type>
71 const typename VariantTypeSelector<_Type, _Types...>::type & get(bool& success) const {
72 // TODO assert _Type in _Types
74 return *(reinterpret_cast<const _Type *>(&valueStorage_));
78 inline bool hasValue() const {
79 return valueType_ < TypesTupleSize::value;
83 // TODO calculate maximum storage
84 std::aligned_storage<80>::type valueStorage_;
89 int main(int argc, char** argv) {
91 Variant<int, double, double, std::string> myVariant(fromInt);
93 const int& myInt = myVariant.get<int>(success);
94 // const float& myFloat = myVariant.get<float>(success);
96 std::cout << "myInt = " << myInt << " (" << std::boolalpha << success << ")\n";