Connecting Tech Pros Worldwide Help | Site Map

template function question

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 23rd, 2005, 04:59 AM
slavinger@yahoo.com
Guest
 
Posts: n/a
Default template function question

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, 04:59 AM
Victor Bazarov
Guest
 
Posts: n/a
Default 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, 04:59 AM
slavinger@yahoo.com
Guest
 
Posts: n/a
Default 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, 05:05 AM
dustmop@gmail.com
Guest
 
Posts: n/a
Default 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.

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.