Connecting Tech Pros Worldwide Help | Site Map

How to get a templated class to determine template argument list on it's own?

Jim Langston
Guest
 
Posts: n/a
#1: May 6 '07
This should illistrate what I am trying to do:

template <class T>
T SomeFunction( T parm )
{
return parm;
}

template <class T>
class SomeClass
{
public:
SomeClass( T fp ) {}
};

int main()
{
int x = 0;
SomeFunction( x );
// SomeClass sf( x ); // Won't work
SomeClass<intsf( x );
}

I'm trying to instantize a templated class without having to specify the
template argument. I can do that for templated functions as shown. When
trying to compile for classes, however, I'm told that:
error C2955: 'SomeClass' : use of class template requires template argument
list

For this simple type of int illistrating the problem, it's no big deal. All
I have to do is add <int>. But what I'm actually trying to do is come up
with a way to store function pointers in a class which this is the first
piece of. I'm don't want to have to figure out if it's int (*)( float,
double, std::string).

I mean, I can in a fuction pass the name of a function and even call it in a
function (with no parms at this point) and I don't have to type in code the
template argument.


Ian Collins
Guest
 
Posts: n/a
#2: May 6 '07

re: How to get a templated class to determine template argument list on it's own?


Jim Langston wrote:
Quote:
This should illistrate what I am trying to do:
>
template <class T>
T SomeFunction( T parm )
{
return parm;
}
>
template <class T>
class SomeClass
{
public:
SomeClass( T fp ) {}
};
>
int main()
{
int x = 0;
SomeFunction( x );
// SomeClass sf( x ); // Won't work
SomeClass<intsf( x );
}
>
I'm trying to instantize a templated class without having to specify the
template argument. I can do that for templated functions as shown. When
trying to compile for classes, however, I'm told that:
error C2955: 'SomeClass' : use of class template requires template argument
list
>
For this simple type of int illistrating the problem, it's no big deal. All
I have to do is add <int>. But what I'm actually trying to do is come up
with a way to store function pointers in a class which this is the first
piece of. I'm don't want to have to figure out if it's int (*)( float,
double, std::string).
>
I mean, I can in a fuction pass the name of a function and even call it in a
function (with no parms at this point) and I don't have to type in code the
template argument.
>
You will have to use a function template to wrap what ever it is you are
doing. Unfortunately, that's the way it is.

--
Ian Collins.
Jim Langston
Guest
 
Posts: n/a
#3: May 6 '07

re: How to get a templated class to determine template argument list on it's own?



"Ian Collins" <ian-news@hotmail.comwrote in message
news:5a4qanF2nopieU8@mid.individual.net...
Quote:
Jim Langston wrote:
Quote:
>This should illistrate what I am trying to do:
>>
>template <class T>
>T SomeFunction( T parm )
>{
> return parm;
>}
>>
>template <class T>
>class SomeClass
>{
>public:
> SomeClass( T fp ) {}
>};
>>
>int main()
>{
> int x = 0;
> SomeFunction( x );
> // SomeClass sf( x ); // Won't work
> SomeClass<intsf( x );
>}
>>
>I'm trying to instantize a templated class without having to specify the
>template argument. I can do that for templated functions as shown. When
>trying to compile for classes, however, I'm told that:
>error C2955: 'SomeClass' : use of class template requires template
>argument
>list
>>
>For this simple type of int illistrating the problem, it's no big deal.
>All
>I have to do is add <int>. But what I'm actually trying to do is come up
>with a way to store function pointers in a class which this is the first
>piece of. I'm don't want to have to figure out if it's int (*)( float,
>double, std::string).
>>
>I mean, I can in a fuction pass the name of a function and even call it
>in a
>function (with no parms at this point) and I don't have to type in code
>the
>template argument.
>>
You will have to use a function template to wrap what ever it is you are
doing. Unfortunately, that's the way it is.
Okay, this actually works as far as it goes, although I didn't think it
would:

#include <iostream>
#include <vector>

class Base
{
virtual ~Base() {}
};

template <class T>
class SomeClass: public Base
{
public:
SomeClass( T fp ) {}
public:
T fp;
};

template <class T>
SomeClass<T>* SomeFunction( T parm )
{
return new SomeClass<T>( parm );
}

int Foo( int Parm )
{
std::cout << "In Foo\n";
return Parm;
}

double Bar( )
{
std::cout << "In Bar\n";
return 3.1415926;
}

void FooBar( int a, double b, std::string c )
{
std::cout << "In FooBar\n";
return;
}

int main()
{
std::vector<Base*Functions;

Functions.push_back( SomeFunction( Foo ) );
Functions.push_back( SomeFunction( Bar ) );
Functions.push_back( SomeFunction( FooBar ) );
}

Now I just have to figure out some mechanism to call fp( parms ) in the
derived classes :/ I've been scratching my head on this one.


Ian Collins
Guest
 
Posts: n/a
#4: May 6 '07

re: How to get a templated class to determine template argument list on it's own?


Jim Langston wrote:
Quote:
>
Okay, this actually works as far as it goes, although I didn't think it
would:
>
#include <iostream>
#include <vector>
>
class Base
{
virtual ~Base() {}
};
>
template <class T>
class SomeClass: public Base
{
public:
SomeClass( T fp ) {}
public:
T fp;
};
>
template <class T>
SomeClass<T>* SomeFunction( T parm )
{
return new SomeClass<T>( parm );
}
>
int Foo( int Parm )
{
std::cout << "In Foo\n";
return Parm;
}
>
double Bar( )
{
std::cout << "In Bar\n";
return 3.1415926;
}
>
void FooBar( int a, double b, std::string c )
{
std::cout << "In FooBar\n";
return;
}
>
int main()
{
std::vector<Base*Functions;
>
Functions.push_back( SomeFunction( Foo ) );
Functions.push_back( SomeFunction( Bar ) );
Functions.push_back( SomeFunction( FooBar ) );
}
>
Now I just have to figure out some mechanism to call fp( parms ) in the
derived classes :/ I've been scratching my head on this one.
>
You will require a family of SomeFunction function templates and
SomeClass class templates, one for each n where n is the umber of
parameters you want to accept.

--
Ian Collins.
Thomas J. Gritzan
Guest
 
Posts: n/a
#5: May 6 '07

re: How to get a templated class to determine template argument list on it's own?


Jim Langston wrote:
[...]
Quote:
int Foo( int Parm )
[...]
Quote:
double Bar( )
[...]
Quote:
void FooBar( int a, double b, std::string c )
[...]
Quote:
int main()
{
std::vector<Base*Functions;
>
Functions.push_back( SomeFunction( Foo ) );
Functions.push_back( SomeFunction( Bar ) );
Functions.push_back( SomeFunction( FooBar ) );
}
>
Now I just have to figure out some mechanism to call fp( parms ) in the
derived classes :/ I've been scratching my head on this one.
Take a look at boost::function (or tr1::function). Its a polymorphic
wrapper for function pointers and functors.

--
Thomas
http://www.netmeister.org/news/learn2quote.html
Closed Thread