Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Template fns in non-template class

Reply
Thread Tools

Template fns in non-template class

 
 
garfunkelorama@googlemail.com
Guest
Posts: n/a
 
      02-01-2007
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.

 
Reply With Quote
 
 
 
 
=?iso-8859-1?q?Erik_Wikstr=F6m?=
Guest
Posts: n/a
 
      02-01-2007
On Feb 1, 1:38 pm, (E-Mail Removed) wrote:
> 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.

--
Erik Wikström

 
Reply With Quote
 
 
 
 
garfunkelorama@googlemail.com
Guest
Posts: n/a
 
      02-01-2007
On 1 Feb, 13:01, "Erik Wikström" <(E-Mail Removed)> wrote:
> On Feb 1, 1:38 pm, (E-Mail Removed) wrote:
>
>
>
>
>
> > 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.
>
> --
> Erik Wikström- Hide quoted text -
>
> - Show quoted text -


Thanks, I think that is the only way too.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Declaring a template class with two template params a friend in anon-template class A L C++ 1 08-25-2010 07:25 AM
template template arguments: expected a class template, got `Component<T1, T2, T3> gary.bernstein@gmail.com C++ 1 06-08-2007 07:10 AM
Transport and inertial delay , resolution fns kitcha VHDL 3 11-24-2005 02:09 AM
A parameterized class (i.e. template class / class template) is not a class? christopher diggins C++ 16 05-04-2005 12:26 AM
member fns w/o args? Steve Canfield Python 2 07-09-2004 05:52 PM



Advertisments