In article <11************ **********@f14g 2000cwb.googleg roups.com>,
"Joseph Turian" <tu****@gmail.c om> wrote:
template <typename T>
foo<T>::foo()
{
}
template <>
foo<int>::foo()
{
cerr << "int\n";
}
So foo<T>::foo is used unless it is overloaded (by foo<int>::foo, in
this circumstance).
If I switched the order in which the constructors were declared, would
foo<T> always be used, or would it be identical?
I believe the order of definition is irrelevant.
foo_imp(std::tr 1::is_same<T, int>());
This is weird, I'll have to google tr1.
Does it only work for primitive types, or will it also work for
user-defined classes?
As someone else mentioned, you can sub in boost::type_tra its if you
don't have tr1 yet (
www.boost.org).
Here is the latest documentation for tr1:
http://www.open-std.org/jtc1/sc22/wg...2005/n1836.pdf
And here's a link to the freely available boost library which inspired
this part of tr1:
http://www.boost.org/doc/html/boost_typetraits.html
is_same<T, U> will work for arbitrary types. It is both clever and
simple:
template <class T, class U> struct is_same
: public integral_consta nt<bool, false> {};
template <class T> struct is_same<T, T>
: public integral_consta nt<bool, true> {};
where integral_consta nt is just a helper class:
template <class T, T v>
struct integral_consta nt
{
static const T value = v;
typedef T value_type;
typedef integral_consta nt<T, v> type;
};
typedef integral_consta nt<bool, true> true_type;
typedef integral_consta nt<bool, false> false_type;
Be aware that const T and T are two different types according to
is_same. But the same type_traits package can strip cv-qualifiers.
The type_traits lib is invaluable for making compile-time
decisions/computations on template parameters. If it helps, here's a
jpeg representation of the type hierarchy which type_traits implements.
http://home.twcny.rr.com/hinnant/cpp...eHiearchy.jpeg
This also roughly follows the classification laid out in section 3.9 of
the standard (modulo those parts the jpeg marks as "proposed" which are
also absent in the boost and tr1 libs).
-Howard