Search code examples
c++templatesusing

C++ multiple definitions of 'using' alias


Is it legal in C++ to declare same using alias multiple times? I have a template library, where one header file is used as 'public' - it contains all template class declarations and aliases, and then 'implementation' files (not in literal sense, these contains definitions of template classes). Public header file includes all implementation headers at it's end.

It is not possible to include public header from implementation file, because that would cause cyclic dependency. However, I want to use template aliases declared in header file inside of implementation file.

Example:

'public' header:

using true_type = logical_constant<true>;
using false_type = logical_constant<false>;

'private' implementation files:

using true_type = logical_constant<true>;
using false_type = logical_constant<false>;

As long as both using aliases alias same thing, is this legal? This simple example worked for me in MSVC 14, but when I tried to use more complicated aliases same way, compiler complained.


Solution

  • Is it legal in C++ to declare same using alias multiple times?

    Yes, it is legal.

    An alias-declaration with the using ... = ...; syntax is a fancier way to declare a typedef. Hence, it can be repeated without any problem as long as they don't declare the alias to be different types.