cppvariant

cppvariant


This is an implementation of an intelligent variant type in C++.

This is similar but much different than boost's any. With boost's any you can store any type, however, boost doesn't provide an implementation for type juggling.

Let's say for example you try this:

The compilation will fail. However with this variant type you can do this:

If you have a specific object you want store you can specify that inside of the template:

class A { public: void f() { } };
variant a = new A();

Note: You don't have to worry about deleting the object as that will happen in the variant destructor.

This variant does have some limitations.

For example you can't do this:

variant a = new A();
variant b = 123;
variant a2 = b;

But you can do this:

variant a = new A();
variant b = 123;
variant a2 = (int)b;

Also you can't mix types like this:

variant a = new A();
variant b = new B();
variant c;
c = b;

This is because in the backend a has a pointer to A and B has a pointer to B. It would be invalid to do something like this:

A * a = new A();
B * b = a;

What you could leverage is polymorphism:

class A { public: void woof() { cout << "woof" << endl; } };
class B : public A { public: void woof() { cout << "woof2" << endl; } };

variant a = new A();
variant b = new B();
a = (A*)b;

Development Team
Admins
Natalie Adams

Downloads:
0
Reviews:
0
Commits:
1
Issues:
0
Documentations:
0

Powered by InDefero,
a CĂ©ondo Ltd initiative.
srchub.org is ran by
Nathan Adams.
Page rendered in 0.03231s using 45 queries.