4

HI Could anyone give a sample program to implement the is_same_type type trait in c++?

Eternal Learner
  • 3,800
  • 13
  • 48
  • 78
  • 1
    Sure, if you provide a detailed specification of what such a program should do. We all sit around here waiting for requests just like this - give us the spec and we will be right on it! –  Apr 14 '10 at 17:28
  • 1
    This isn't really a "write the program for me" as much as a "I heard about this thing and I have no idea how to do it, but I think I need it" request. Agreed; its not the best question in the world, but I don't think it really violates any guidelines, except not being very specific. – i_am_jorf Apr 14 '10 at 17:35

1 Answers1

12
#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
  • 219,715
  • 46
  • 258
  • 445
  • Why `enum {result = true}`, rather than `static const bool result = true`? Won't the `enum` give `result` the wrong type? – Brooks Moses Apr 14 '10 at 17:30
  • 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. –  Apr 14 '10 at 17:31
  • @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 Apr 14 '10 at 17:33
  • 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. – i_am_jorf Apr 14 '10 at 17:34
  • 4
    @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 Apr 14 '10 at 17:37
  • @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. –  Apr 14 '10 at 17:45
  • 1
    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 Apr 14 '10 at 17:53
  • 1
    @Srinivasa: Get a copy of *"Modern C++ programming"*, it should get you started. – Georg Fritzsche Apr 14 '10 at 17:59
  • 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. Apr 14 '10 at 18:01
  • 1
    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 Apr 14 '10 at 18:04
  • 3
    @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 Apr 14 '10 at 18:09
  • 2
    @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 Apr 14 '10 at 20:05