1

Here is what I tested through http://webcompiler.cloudapp.net/ (live development branch for the Visual C++ compiler) :

#include <iostream>

using namespace std;

// the real stuff begins here
template < typename T > using is_char_t                  = int;
template <            > using is_char_t<          char > = char;
template <            > using is_char_t< unsigned char > = char;
// the real stuff ends here

template < typename T > bool is_char(T const &)
{
    return sizeof(is_char_t< T >) == 1;
}

int main()
{
   cout << "is_char( 1.0) = " <<is_char(1.0) << endl; 
   cout << "is_char(   1) = " <<is_char(1) << endl; 
   cout << "is_char('1' ) = " <<is_char('1') << endl; 
   cout << "is_char('1'U) = " <<is_char(unsigned char('1')) << endl; 
}

Doing so, I got:

Compiled with /EHsc /nologo /W4 main.cpp

main.cpp(7): error C2061: syntax error: identifier 'is_char_t'

main.cpp(8): error C2061: syntax error: identifier 'is_char_t'

Well I was not expecting for it to work.

Of course, I can reach the same goal with more boilerplate code:

namespace internal
{
    template < typename T > struct is_char_t                  { using type = int; };
    template <            > struct is_char_t<          char > { using type = char; };
    template <            > struct is_char_t< unsigned char > { using type = char; };
}
template < typename T > using is_char_t = typename internal::is_char_t< T >::type;

So my question is: is it an oversight or are there any reason that specializing a partial template type alias is too much complex to accept it in C++ grammar when "using" was extended to define a type alias ?

Community
  • 1
  • 1
hlide
  • 237
  • 2
  • 10
  • 1
    Is this answered here: http://stackoverflow.com/a/6623089/777186 ? (Sorry if I misunderstood). – jogojapan Jan 04 '15 at 15:10
  • @jogojapan: You beat me to it! I think that (three-year-old) post answers the OP's question. (Briefly: Yes, there is a reason.) – TonyK Jan 04 '15 at 15:12
  • well, I suspect there is an answer but it horribly lacks a simple example and the answer is not that clear and simple as I would expect. The only answer I can grab there is: `Because an alias-declaration cannot declare a template-id, it is not possible to partially or explicitly specialize an alias template.` – hlide Jan 04 '15 at 15:44

0 Answers0