HI Could anyone give a sample program to implement the is_same_type type trait in c++?
+5
A:
#include <iostream>
template< typename T1, typename T2 >
struct is_same_type { enum { result = false }; };
template< typename T>
struct is_same_type<T,T> { enum { result = true }; };
int main()
{
std::cout << is_same_type<int,float>::result << '\n'
<< is_same_type<char,char>::result << '\n';
return 0;
}
sbi
2010-04-14 17:28:55
Why `enum {result = true}`, rather than `static const bool result = true`? Won't the `enum` give `result` the wrong type?
Brooks Moses
2010-04-14 17:30:47
So you are setting yourself up as a program writing service now? This really doesn't help the OP (or you) in the long run.
anon
2010-04-14 17:31:46
@Brooks: The only reason is that I did TMP when the `enum` trick was still hip and more portable anyway. I agree that nowadays it should be `static const bool result = ...`, but I keep forgetting this...
sbi
2010-04-14 17:33:11
I'm curious about the enum {} thing too. I've seen this in other code but have only been able to speculate about why its happening.
jeffamaphone
2010-04-14 17:34:10
@Neil: Usually I'm among the first to jump on anyone who seems to post homework questions without a proper tag, but I seriously doubt anyone but me would give their students a TMP problem for homework. And, yes, absent of the homework problem, if people politely ask whether someone could show them something, and if that something is easy enough so that I happen to able to be make it up in <3mins, I don't usually hesitate to show them. That (and the fact that others are quick in pointing out my errors) is what I'm here for, after all.
sbi
2010-04-14 17:37:05
@jeffamaphone: See e.g. http://stackoverflow.com/questions/2172647/
Georg Fritzsche
2010-04-14 17:37:13
@sbi Well, I disagree. I too routinely provide provide compilable C++ code in my answers (in fact I always try to ensure the code is compilable - don't always succeed) but ONLY if the OP also put in some effort. But of course you must reply to posts as you see fit.
anon
2010-04-14 17:45:42
Hi this by no means is an Home Work.. I am just very confused about Templates and Type Traits.. I tried looking up a lot of sites but have not found a single one which in simple correct term explain type traits or Templates.. I decided the next best way to learn is to read from code samples and try to figure out the pieces.. If any body experienced have a better way to understand Meta Programming and Type traits I will be glad and thankful..
Eternal Learner
2010-04-14 17:53:35
@Srinivasa: Get a copy of *"Modern C++ programming"*, it should get you started.
Georg Fritzsche
2010-04-14 17:59:15
You could try some books. If you are comfortable with templates in C++, Meta Template Programming by David Abrahams and Alexei Gurtovoy is a must read :)
Matthieu M.
2010-04-14 18:01:28
I am a beginner to Template Programming in C++ and it looks and sounds so very different from all the c++ I had learned earlier.
Eternal Learner
2010-04-14 18:04:46
@Srinivasa: Then maybe a more basic book would be better (like *"C++ Templates The Complete Guide"*) - note that there is a C++ book list on SO: http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list
Georg Fritzsche
2010-04-14 18:09:01
@Srinivasa: If you want to learn about traits, Nathan Myers' classic http://www.cantrip.org/traits.html might be the first publication on it. I second the recommendation of _C++ Templates The Complete Guide_. It's got all the basics you need to understand templates. You can take it from there. Both _Modern C++ Design_ and _C++ Template Metaprogramming_ are great books, but might be a bit tall for someone who doesn't know enough of the basics of templates.
sbi
2010-04-14 20:05:32
Thanks..Will look into that..
Eternal Learner
2010-04-14 20:28:20