Template fns in non-template class

G

garfunkelorama

Hi all,

I'm sure there is a simple solution to this but it's beyond me. Please
can someone help?
You have a class like this:

class A
{
public:
template<typename T>
void f(key &k, T& t)
{
// need to store the address of t
}

void g(key &k)
{
// somehow get the address of the t variable above from the
supplied key
}
};

and then usage could be:

A a;
string str = "hello";
double d = 3.2;
int i = 2;

a.f(1, str);
a.f (2, d);
a.f(3, i);
//....

a.g(); //do something to alter values of str, d, i

How can you store the variable t but make it accessible to the rest of
the class ? You cannot have any variable in the class mention T as
it's not a template class. No void ptr if possible as that has its
associated headaches.
The boost "variant" library looks helpful but still i can't see how.
Other than resort to scrapping the template and just have regular int,
double and string pointers within A.

Thank you.
G.
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

Hi all,

I'm sure there is a simple solution to this but it's beyond me. Please
can someone help?
You have a class like this:

class A
{
public:
template<typename T>
void f(key &k, T& t)
{
// need to store the address of t
}

void g(key &k)
{
// somehow get the address of the t variable above from the
supplied key
}

};

and then usage could be:

A a;
string str = "hello";
double d = 3.2;
int i = 2;

a.f(1, str);
a.f (2, d);
a.f(3, i);
//....

a.g(); //do something to alter values of str, d, i

How can you store the variable t but make it accessible to the rest of
the class ? You cannot have any variable in the class mention T as
it's not a template class. No void ptr if possible as that has its
associated headaches.
The boost "variant" library looks helpful but still i can't see how.
Other than resort to scrapping the template and just have regular int,
double and string pointers within A.

You have to cast it to void*, something like this ought to work:

class A {
std::map<key, void*> map_;
public:
template<class T>
void f(key k, T& t) {
map_[k] = static_cast<void*>(&t);
}
void* g(key k) {
return map_[k];
}
};

The problem with this is that the user calling g() have to know the
type of the object pointed to, but if it's known then you can cast it
back to a pointer of string, int, or whatever.
 
G

garfunkelorama

I'm sure there is a simple solution to this but it's beyond me. Please
can someone help?
You have a class like this:
class A
{
public:
template<typename T>
void f(key &k, T& t)
{
// need to store the address of t
}
void g(key &k)
{
// somehow get the address of the t variable above from the
supplied key
}

and then usage could be:
A a;
string str = "hello";
double d = 3.2;
int i = 2;
a.f(1, str);
a.f (2, d);
a.f(3, i);
//....
a.g(); //do something to alter values of str, d, i
How can you store the variable t but make it accessible to the rest of
the class ? You cannot have any variable in the class mention T as
it's not a template class. No void ptr if possible as that has its
associated headaches.
The boost "variant" library looks helpful but still i can't see how.
Other than resort to scrapping the template and just have regular int,
double and string pointers within A.

You have to cast it to void*, something like this ought to work:

class A {
std::map<key, void*> map_;
public:
template<class T>
void f(key k, T& t) {
map_[k] = static_cast<void*>(&t);
}
void* g(key k) {
return map_[k];
}

};

The problem with this is that the user calling g() have to know the
type of the object pointed to, but if it's known then you can cast it
back to a pointer of string, int, or whatever.

Thanks, I think that is the only way too.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top