In article <1136065266.523197.164230@f14g2000cwb.googlegroups .com>,
"Joseph Turian" <turian@gmail.com> wrote:
[color=blue][color=green]
> > template <typename T>
> > foo<T>::foo()
> > {
> > }
> >
> > template <>
> > foo<int>::foo()
> > {
> > cerr << "int\n";
> > }[/color]
>
> 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?[/color]
I believe the order of definition is irrelevant.
[color=blue]
>[color=green]
> > foo_imp(std::tr1::is_same<T, int>());[/color]
>
> 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?[/color]
As someone else mentioned, you can sub in boost::type_traits 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_constant<bool, false> {};
template <class T> struct is_same<T, T>
: public integral_constant<bool, true> {};
where integral_constant is just a helper class:
template <class T, T v>
struct integral_constant
{
static const T value = v;
typedef T value_type;
typedef integral_constant<T, v> type;
};
typedef integral_constant<bool, true> true_type;
typedef integral_constant<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