c++c++11function-templatestemplate-aliases

C++ 11 conditional template alias to function


In C++ 11, I want to make a template alias with two specializations that resolve to a different function each.

void functionA();
void functionB();

template<typename T = char>
using Loc_snprintf = functionA;

template<>
using Loc_snprintf<wchar_t> = functionB;

So I can call e.g. Loc_snprintf<>() and it's resolve to functionA().

Apparently seems impossible (to compile). Is there something ultimately simple that mimics it (maybe using a class template)?


Solution

  • In C++11 it's not really possible to create aliases of specializations. You must create actual specializations:

    template<typename T = char>
    void Loc_snprintf()
    {
        functionA();
    }
    
    template<>
    void Loc_snprintf<wchar_t>()
    {
        functionB();
    }
    

    With C++14 it would be possible to use variable templates to create a kind of alias.