473,386 Members | 1,706 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,386 software developers and data experts.

How to use Templates to return different types

i.e. getmax(long, int) or getmax(int, long) will return in long data type?

and how do we check what data type something runtime in c++?

thanks in advance for any help.
Jul 23 '05 #1
1 1233
* kien:
i.e. getmax(long, int) or getmax(int, long) will return in long data type?
You can't do that easily in the general case, but for the specific case
you can simply define a non-template overload:

long mymax( long a, long b ){ return mymax<long>( a, b ); }

which invokes the usual integer promotion rules for the arguments.

That doesn't work in the general case because with more than one overload
the arguments don't unambigiously select just one of them.

For the general case I guess you could make a macro that expanded the simple
call to something. But the sun's been turned on outside, so I think I'll
check out the fine wheather rather than tinkering with this problem. No way
to know when they'll turn it off again.

Btw., style, when did you last see "getsin", "getcos", and so forth in the
standard lib?

and how do we check what data type something runtime in c++?


At runtime, typeid, dynamic_cast, and RTTI in general.

Doesn't help you with template parameters, though, since they are compile-time.

And usually it's much better to use virtual functions than manual type checking.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #2

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

Similar topics

3
by: Tim Wesson | last post by:
Does anyone know if it is possible to choose between the below templates (or similar ones) according to the promotion rules for types T and U? Thanks, Tim Wesson.
3
by: Andre | last post by:
Hi, I've built a math library that does some matrix multiplications and other linear algebric functions. I read some place about generics or class templates and I was wondering if this will help...
9
by: Anthony Heading | last post by:
Hi all, I've often found myself wanting to write code like the example here. Since both MSVC and gcc both reject it, I suspect it is indeed illegal. gcc: no type named `Name' in `class...
12
by: Simon | last post by:
Hi, I'm having a problem with templates and specialisation. I'm using it to overload the same function so it can return different things. I can't see what I'm doing wrong, although my compiler...
2
by: kelvSYC | last post by:
I'm trying to program something along the lines of a "trading card" idea: I have a single copy of the "card" in the program, yet there may be multiple "instances" of the "card", with differing...
7
by: Jürgen Kaminski | last post by:
Hello all, I got some legacy code where a template implements a non-templated interface. These classes are really HUGE, but it boils down to the following structure: class BaseClass { public:...
1
by: Bo Xu | last post by:
Object of Combination By Bo Xu Introduction A combination of n things, taken s at a time, often referred as an s-combination out of n, is a way to select a subset of size s from a given set of...
25
by: Ted | last post by:
I'm putting the posts that follow here (hopefully they will follow here!) because they were rejected in comp.lang.c++.moderated. It behooves anyone reading them to first read the the thread of the...
8
by: collection60 | last post by:
Hi people, I want to use templates, for syntatic niceness only, not for doing similar operations on different types. The types I'm manipulating are basically just pointers. So if it's a pointer...
23
by: Ben Voigt | last post by:
I have a POD type with a private destructor. There are a whole hierarchy of derived POD types, all meant to be freed using a public member function Destroy in the base class. I get warning C4624....
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
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...
0
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
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...

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.