about variant type

Nov 22, 2013 at 12:28pm
i'm building the variant type:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <cstdlib>
#include <iostream>
#include <string>
#ifndef VARIANT_H_INCLUDED
#define VARIANT_H_INCLUDED

class variant
{
    string a="";
 public:

    variant (string value="")
    {
        a=value;
    }

    variant (double value)
    {
        a=to_string(value);
    }

    friend istream& operator >>(istream &is,variant &obj)
    {
        is>>obj.a;
        return is;
    }

    friend ostream& operator <<(ostream &os,const variant &obj)
    {
        os<<obj.a;
        return os;
    }

    friend istream &getline(istream &in, variant &s1)
    {
        getline(in, s1.a);
        return in;
    }


    variant & operator = (int const & b)
    {
        a=to_string(b);
       return *this;
    }

    variant & operator = (string const & b)
    {
        a=b;
        return *this;
    }

    variant & operator = (double const & b)
    {
        a=to_string(b);
        return *this;
    }

    variant & operator = (float const & b)
    {
        a=to_string(b);
        return *this;
    }

    bool operator == (string const & b)
    {

        return  (a==b);
    }

    operator string() const
    {
        return a; // return string member
    }

    operator double() const
    {
        return atof(a.c_str()) ;
    }

};

#endif // VARIANT_H_INCLUDED 

but don't accept class\struct\enum or others.
i think that i can use the:
void * b;
but if theres so many types and we can create a new types, how can i prepare the 'b' for reacive the values?
Last edited on Nov 22, 2013 at 12:29pm
Nov 22, 2013 at 12:37pm
It's common to create a variant from a Union with a Tag that identifies what field of the union is being used.

You've used a string as the implementation, presumably to convert that content to/from the externally exposed type.
Nov 22, 2013 at 12:41pm
even using a Union, how you can prepare for a class's\struct\enum and others?
Nov 22, 2013 at 12:51pm
how you can prepare for a class's\struct\enum and others?
That's what templates are for. See:

http://www.cplusplus.com/doc/tutorial/templates/


boost has already a variant:

http://www.boost.org/doc/libs/1_55_0/doc/html/variant.html
Nov 22, 2013 at 12:57pm
let me ask you more 1 thing: using templates, i must overload the operators too(+,- and others)?
Nov 22, 2013 at 1:10pm
using templates, i must overload the operators too(+,- and others)?
No, templates don't have any particular requirements.

The point with templates is that you declare a kind of placeholder for a type that you need to define when you use that class\struct\function
Nov 22, 2013 at 1:18pm
thanks for all.. thanks to all
Nov 22, 2013 at 3:31pm
heres the template class:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
template <typename a>
class variant
{
private:
    a Value;
public:

    variant(a value)
    {
        Value=value;
    }
    variant()
    {
    }
};

variant<int> e=10;

i see 2 problems:
1 - with cout: "error: cannot bind 'std::ostream {aka std::basic_ostream<char>}' lvalue to 'std::basic_ostream<char>&&'";

2 - imagine that i need change the type for other situation, how can i do it?
Topic archived. No new replies allowed.