473,508 Members | 2,206 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.

Jul 23 '05 #1
3 1099
sl*******@yahoo.com wrote:
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
Jul 23 '05 #2
Victor Bazarov wrote:

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.

Jul 23 '05 #3
sl*******@yahoo.com wrote:
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.

Jul 23 '05 #4

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

Similar topics

6
2005
by: Dave | last post by:
Hello all, Consider this function template definition: template<typename T> void foo(T) {} If foo is never called, this template will never be instantiated. Now consider this explicit...
3
2063
by: 胡岳偉(Yueh-Wei Hu) | last post by:
Hi all, I have 2 questions about template function as friends in template classes. I don't know why, and hope someone could help me. ...
0
1678
by: Yueh-Wei Hu | last post by:
Victor Bazarov <v.Abazarov@comAcast.net> wrote in message news: ============================================================== > > Question 1: > >...
4
2333
by: Thomi Richards | last post by:
Hi, I'm trying to create a simple stack class using C++ and templates. Everything works well and good if I amke the class totally inline. However, as soon as I try to seperate the class into a...
5
1212
by: wongjoekmeu | last post by:
Hello all, I have a question about templates. Let say I have the following template function ----- template <class T> T max(T a, T b) { return a > b ? a : b ; }
2
2298
by: pookiebearbottom | last post by:
Just trying to learn some things about templates. Was wondering how boost::tupple really works, but the headers were a bit confusing to me. I know you get do something like the following, just...
5
2251
by: StephQ | last post by:
This is from a thread that I posted on another forum some days ago. I didn't get any response, so I'm proposing it in this ng in hope of better luck :) The standard explanation is that pointer...
6
1716
by: Bartholomew Simpson | last post by:
I'm trying to avoid (or at least, minimize) duplicity of effort. I have the following function: void Permissions::Exists(const unsigned int id, std::vector<Permission>::const_iterator& iter) {...
4
3003
by: David Sanders | last post by:
Hi, I have a class with an integer template parameter, taking values 1, 2 or 3, and a function 'calc' in that class which performs calculations. Some calculations need only be performed if the...
21
4659
by: H9XLrv5oXVNvHiUI | last post by:
Hi, I have a question about injecting friend functions within template classes. My question is specific to gcc (version 3.4.5) used in combination with mingw because this code (or at least code...
0
7225
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7326
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7383
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5627
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5053
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
3194
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1557
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
766
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
418
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.