template function question

S

slavinger

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.
 
V

Victor Bazarov

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.

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
 
S

slavinger

Victor said:
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.

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.
 
D

dustmop

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.

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.
 

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
474,206
Messages
2,571,069
Members
47,675
Latest member
KevinStepp

Latest Threads

Top