An empty structure or anonymous structure as a tag

Is there a difference in usage between defining a tag type as an anonymous empty structure or an empty structure?

using A = struct {}; struct B {}; 

In my mind, the only difference is the β€œeffective” type name when some kind of reflection is used (ie __PRETTY_FUNCTION__ , <cxxabi.h>:abi::__cxa_demangle(typeid().name()) , etc. )

ADL works in both directions:

 namespace ns { using A = struct {}; struct B {}; constexpr bool adl(A) { return true; } constexpr bool adl(B) { return true; } } template< typename type > constexpr bool adl(type) { return false; } static_assert(adl(ns::A{})); static_assert(adl(ns::B{})); 
+8
c ++ c ++ 11 tags c ++ 14
source share
1 answer

Besides the different lines that you have already noted, the only significant difference is that you can refer to B using a specifier of a specified type, so you can say struct B b; instead of B b; but you cannot use struct A a; because A is a typedef name, not a class name.

However, there is almost no good reason to say struct B instead of just B , so in practice the difference is not important, especially not for tag types.

+5
source share

All Articles