[Cs3] is_same

Mikhail Nesterenko mikhail at cs.kent.edu
Sat Mar 1 10:50:55 EST 2014


CS3 students,

Folks were asking whether is is possible to determine the type the
template is initialized with. C++11 standard provides a mechanism
named is_same that does this. The syntax is this

  is_same<Type1,Type2>::value

It is a structure whose constant "value" is true or false depending on
whether the Type1 is the same as Type2. is_same requires type_traits
header file. I provided "checkType" example that uses is_same in the
Templates Example directory of the course's website.

   http://www.cs.kent.edu/~mikhail/classes/cs3.s14/Examples/Templates/checkType.cpp

In general, is_same is considered a crutch. Type differentiation
should be achieved through specialization mechanism.

Also, g++ with versions below 4.7 (this includes departmental unix
servers) supports C++11 features like is_same only with -std=c++0x
option.

Thanks,
-- 
Mikhail


More information about the cs3 mailing list