Re: Help: How many explicit specializations required?

From: CoolPint (coolpint_at_yahoo.co.uk)
Date: 09/16/04


Date: 16 Sep 2004 06:48:45 -0700

I just figured out how to differentiate between T and array of T. Not
only that, I also learned I had to differentiate between array of T
and array of const T, too.

Now, that involves a lot of specialization. I am now a bit clearer
about my originial questions B and C, but I still wonder about
combining the four separate specializations required for handing
C-style char strings.

I still very much love to hear ideas from experts. Thank you again.

Below is the program I modified from the previous post to show me
what's going on.

#include <iostream>
using std::cout;

template <typename T>
void func(const T &)
{ cout << "primary\n" ; }

template <typename T>
void func(T * const &) // should handle T * as well as const T *
{ cout << "Overloading for Pointers\n"; }

template <>
void func(char * const &)
{ cout << "specialization: char * \n"; }

template <>
void func(const char * const &)
{ cout << "specialization: const char * \n"; }

template <int N>
void func( const char (&) [N] )
{ cout << "Overloading for const char[] \n"; }

template <int N>
void func( char (&) [N] )
{ cout << "Overloading for char[] \n"; }

template <typename T, int N>
void func( T (&) [N] )
{ cout << "Overloading for T [] \n"; }

template <typename T, int N>
void func( const T (&) [N] )
{ cout << "Overloading for const T [] \n"; }

int main()
{
  char * pc = "whatever";
  const char * pcc = "whatever";
  char ac[] = "whatever";
  const char acc[] = "whatever";
  int i;
  const int ci=1;
  int ai[] = { 0 };
  const int aci[] = {0};

  cout << "int: "; func(i);
  cout << "int *: "; func(&i);
  cout << "const int: "; func(ci);
  cout << "const int *: "; func(&ci);
  cout << "int [] : "; func(ai);
  cout << "const int [] :"; func(aci);

  cout << "Literal String: "; func("whatever");
  cout << "char *: "; func(pc);
  cout << "const char *: "; func(pcc);
  cout << "char []: "; func(ac);
  cout << "const char []: "; func(acc);
}



Relevant Pages

  • Re: Difficulty with nested template classes (newbie)
    ... the huge post but I am including first the errors, then the template ... Reference initialized with 'const double', ... matrix& lookup (int row, int col); ... // iterator operator++ ...
    (comp.lang.cpp)
  • template syntax to destinguish pointer and objects..?
    ... template <class T, typename I> ... int IDconst; ... inline void critical_enterconst ...
    (microsoft.public.vc.language)
  • Re: Declaring war on macros
    ... template ... out mixed cases - one const and one non-const argument. ... templates don't play nicely with automatic conversion and ... short int b; ...
    (comp.lang.cpp)
  • why overloading isnt happened?
    ... I have 2 questions about template function as friends in template ... template<int a, int b> ... void func(Tconst * const t) ...
    (comp.lang.cpp)
  • Re: stl template help
    ... // The general template: ... void func() { ... // the special version for type int. ... struct X{ ...
    (alt.comp.lang.learn.c-cpp)