473,385 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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.
May 6 '07 #1
4 3116
Jim Langston wrote:
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.
May 6 '07 #2

"Ian Collins" <ia******@hotmail.comwrote in message
news:5a*************@mid.individual.net...
Jim Langston wrote:
>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.
May 6 '07 #3
Jim Langston wrote:
>
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.
May 6 '07 #4
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.

--
Thomas
http://www.netmeister.org/news/learn2quote.html
May 6 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: tirath | last post by:
Hi all, I have a templated class that derives from a non-templated abstract class. How do I then cast a base class pointer to a <templated> derived class pointer in a generalised fashion? ...
1
by: Rich | last post by:
Hi, I have a query regarding VC6 and its handling of templated copy constructors. Here goes: Take a look at the following code sample... template<class _Ty, size_t t_uiSize = 10 > class...
2
by: Thomas Matthews | last post by:
Hi, I would like to create a table (or vector) of pointers to templated functions. 1. How do I declare a typedef of a pointer to a templated function? For example, I have some functions...
3
by: case2005 | last post by:
Can anyone help with the following, I don't know if it's possible, but I'm certain there must be a standard way of dealing with this. I have the following: template<typename FooBar, typename...
3
by: Lawrence Spector | last post by:
How does one call a templated constructor inside of a class when instantiating an object? I made up a quick sample to demonstrate. #include <iostream> class TestClass { public: template...
7
by: mosfet | last post by:
HI, when trying to compile an embedded version of STL called ustl on win32 platform I get the following error : /// Returns the minimum of \p a and \p b template <typename T1, typename T2>...
2
by: VirGin | last post by:
Hola Folks, I have a class template (or template class, depending on how one was taught). I have a class that is derived from the template class. I have a method in the parent that is overloaded...
2
by: domehead100 | last post by:
I have a templated class, CDerived: template <typename TValue, typename TDraw, typename TEdit ...> class CDerived : public CBase { TValue m_Value public: TValue& GetValue() const {
2
card
by: card | last post by:
Hi everyone, I have a question about referencing a nested class contained within a templated class. Of course the best way to show you is by example. Here's my templated classes: #include...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.