Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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)?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading