Connecting Tech Pros Worldwide Help | Site Map

template function question

  #1  
Old July 23rd, 2005, 05:59 AM
slavinger@yahoo.com
Guest
 
Posts: n/a
Hello,

I was attempting do the following (in VC++):

#define AGE 1
#define NAME 2

template<class T> T getInfo(int what)
{
int age = 20;
string name = "whatever";

if (what == AGE)
return age;
else if (what == NAME)
return name;
}

The idea is that main() can get both kinds of info by calling a single
function, for instance "cout << getInfo(NAME);" I was looking to
implement this function inside a class that has many different types of
private data. The compiler is complaining that it can't deduce the
data type of T. I can see why it's complaining, so I'm wondering: is
there a way to do something like this in C++? Thanks.

  #2  
Old July 23rd, 2005, 05:59 AM
Victor Bazarov
Guest
 
Posts: n/a

re: template function question


slavinger@yahoo.com wrote:[color=blue]
> I was attempting do the following (in VC++):
>
> #define AGE 1
> #define NAME 2
>
> template<class T> T getInfo(int what)
> {
> int age = 20;
> string name = "whatever";
>
> if (what == AGE)
> return age;
> else if (what == NAME)
> return name;
> }
>
> The idea is that main() can get both kinds of info by calling a single
> function, for instance "cout << getInfo(NAME);" I was looking to
> implement this function inside a class that has many different types of
> private data. The compiler is complaining that it can't deduce the
> data type of T. I can see why it's complaining, so I'm wondering: is
> there a way to do something like this in C++? Thanks.[/color]

Well, no, actually. If the argument will be known at compile time,
you don't need a template, you just need a bunch of functions, like

std::string getName();
int getAge();

But if the argument ('what') is not known at compile time, then you
won't be able to do what you think you need:

cout << somefunctionreturningunknowntype(what);

is *not* going to compile for the same reason as

whattypetousehere var = somefunctionreturningunknowntype(what);

C++ is a statically typed language with some features allowing the use
of run-time polymorphism, which, incidentally, still requires some kind
of common type binding all derived types together. If your return values
are as unrelated as 'std::string' and 'int', there is no polymorphism to
speak of.

V
  #3  
Old July 23rd, 2005, 05:59 AM
slavinger@yahoo.com
Guest
 
Posts: n/a

re: template function question


Victor Bazarov wrote:[color=blue]
>
> Well, no, actually. If the argument will be known at compile time,
> you don't need a template, you just need a bunch of functions, like
>
> std::string getName();
> int getAge();
>
> But if the argument ('what') is not known at compile time, then you
> won't be able to do what you think you need:
>
> cout << somefunctionreturningunknowntype(what);
>
> is *not* going to compile for the same reason as
>
> whattypetousehere var = somefunctionreturningunknowntype(what);
>
> C++ is a statically typed language with some features allowing the use
> of run-time polymorphism, which, incidentally, still requires some kind
> of common type binding all derived types together. If your return values
> are as unrelated as 'std::string' and 'int', there is no polymorphism to
> speak of.
>[/color]

OK, my suspicion has been confirmed - thanks. I knew *why* it was
having issues, was just curious if there was a way to get around it.

  #4  
Old July 23rd, 2005, 06:05 AM
dustmop@gmail.com
Guest
 
Posts: n/a

re: template function question


slavinger@yahoo.com wrote:[color=blue]
> Hello,
>
> I was attempting do the following (in VC++):
>
> #define AGE 1
> #define NAME 2
>
> template<class T> T getInfo(int what)
> {
> int age = 20;
> string name = "whatever";
>
> if (what == AGE)
> return age;
> else if (what == NAME)
> return name;
> }
>
> The idea is that main() can get both kinds of info by calling a single
> function, for instance "cout << getInfo(NAME);" I was looking to
> implement this function inside a class that has many different types of
> private data. The compiler is complaining that it can't deduce the
> data type of T. I can see why it's complaining, so I'm wondering: is
> there a way to do something like this in C++? Thanks.[/color]

If this is really a problem that you need to solve, you could always
try something like...


#include <iostream>
#include <string>
using namespace std;

#define AGE ((int *)1)
#define NAME ((string *)2)

template <class T>
T getInfo(T * what)
{
int age = 20;
string name = "whatever";

void * type = (void *)what;
void * ret = 0;

if (type == (void *)AGE) {
ret = &age;
} else if (type == (void *)NAME) {
ret = &name;
}

return *((T *)ret);
}

int main() {

cout << getInfo(AGE) << endl;

cout << getInfo(NAME) << endl;

}


No guarantees as to definedness or portability, however.

Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
How can I declare and define a friend template function in a template class? =?gb2312?B?wfXquw==?= answers 7 August 1st, 2007 02:55 AM
specialization of a template function in a template class?!?!? pookiebearbottom@yahoo.com answers 2 August 7th, 2006 02:05 PM
Template function question Steve Schlesinger answers 5 May 4th, 2006 05:45 AM
specialize a template function that contains a template parameter sebastian answers 1 September 17th, 2005 05:25 PM