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

J

Jim Langston

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<int> sf( 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.
 
I

Ian Collins

Jim said:
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<int> sf( 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.
 
J

Jim Langston

Ian Collins said:
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.
 
I

Ian Collins

Jim said:
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.
 
T

Thomas J. Gritzan

Jim Langston wrote:
[...]
int Foo( int Parm ) [...]
double Bar( ) [...]
void FooBar( int a, double b, std::string c ) [...]
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.
 

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,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top