Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Empty struct or anonymous struct as tag

Is there any difference in a use between defining the tag type as an anonymous empty struct or as an empty struct?

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

At my mind the only difference is the "effective" type name, when one utilize a kind of reflection (i.e. __PRETTY_FUNCTION__, <cxxabi.h>:abi::__cxa_demangle(typeid().name()) etc).

ADL work for both ways:

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{}));
like image 808
Tomilov Anatoliy Avatar asked Jun 19 '15 06:06

Tomilov Anatoliy


1 Answers

Apart from the different strings you've already noted, the only significant difference is that you can refer to B using an elaborated-type-specifier, 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 never a good reason to say struct B instead of just B so in practice the difference is not important, especially not for tag types.

like image 188
Jonathan Wakely Avatar answered Sep 22 '22 06:09

Jonathan Wakely