container to hold numerous data types

Pages: 12
@ResidentBiscuit:
I have to ask, what situation is this?


to limit the off topic discussion, you can visit my question thread:
http://www.cplusplus.com/forum/beginner/106231/

@Smac89:
i don't think template would give the desired behavior, because template are translated into normal classes at compile time.

OP wants some type that is generic at runtime, am i right?
@cire

Is there a way to fix this or is it just not possible with c++?
Is there a way to fix this or is it just not possible with c++?


Did you check out Boost.Any as mentioned upthread?

http://www.boost.org/doc/libs/1_54_0/doc/html/any.html
yeah i had my answer with boost::any, but i was stuck around to see if you guys posted a standard method that you could manipulate or something.
You could use a struct to hold all the data types you would encounter.
@pata:
You could use a struct to hold all the data types you would encounter.


i think that would be much impractical.
You could use a struct to hold all the data types you would encounter.

Indeed, I've used such an approach myself. I wanted the ability to store a character or a number (actually a few other types as well). i used a struct with one additional field, which indicated how that particular instance was to be interpreted.
> i think that would be much impractical.

The practical solution was given in the first reply posted by rafae11.

If you know the set of possible types before hand, use boost::variant<>
If not, use boost::any


> in my current project, i needed a class member that is publicly accessible for read operations,
> but private for write operations,

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
#include <iostream>

// use the preprocessor
#define PUBLIC_CONST( Type, Name, Initial_value ) \
    public: const Type& Name = Name##_ ; \
    private: Type Name##_ = Initial_value ;\
    public:

// or use a templated type holder
template < typename C, typename T > struct public_const_
{
    operator const T& () const { return v ; }
    private:
        T v ;
        T& operator()() { return v ; }
        public_const_( const T& vv = T() ) : v(vv) {}
        friend C ; // C++11
};

struct A
{
    void foo() { count_ = 9999 ; value() = 99.99 ; }

    PUBLIC_CONST( int, count, 1234 ) // preprocessor

    template < typename T > using public_const = public_const_<A,T> ;

    public_const<double> value = 12.34 ; // template
};

int main()
{
    A a ;
    std::cout << a.count << ' ' << a.value << '\n' ;

    a.foo() ;
    std::cout << a.count << ' ' << a.value << '\n' ;
}

http://ideone.com/okxxup
I was playing around, you would need to keep track of what is stored where but this works as a "generic container"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <vector>
#include <string>

using namespace std;
void enter(void* type_value);

vector<void*> vpv;
int main(){
    int i = 8;
    double d = 3.0;
    char c = 'X';
    string s = "Pata";
   vpv.push_back(&i);
   vpv.push_back(&d);
   vpv.push_back(&c);
   vpv.push_back(&s);
   enter(vpv[3]);

}

void enter(void* type_value){
    cout << *static_cast<string*>(type_value) << endl;
}
I was playing around, you would need to keep track of what is stored where but this works as a "generic container"


Not really. The only thing the container can hold are void pointers. One can't do, for instance, vpv.push_back(std::string()).
Topic archived. No new replies allowed.
Pages: 12