c++c++17inline-variable

Defining global constexpr variables in anonymous namespace the same as making them inline?


Following up on Why does cppreference define type_traits xxx_v shortcuts as inline constexpr and not just constexpr?, if I make my own type trait and want to avoid ODR violations and want it to be compatible with pre-C++17 projects, is putting the xxx_v shortcut in an anonymous namespace the same as explicitly declaring it inline?

For example, taking all_true from Check traits for all variadic template arguments, with C++17 I can write in my utility header:

template <bool...> struct bool_pack;
template <bool... v>
using all_true = std::is_same<bool_pack<true, v...>, bool_pack<v..., true>>;
template <bool... v>
inline constexpr bool all_true_v = all_true<v...>::value;

Is that the same as writing the following code which is compatible with pre-C++17?

template <bool...> struct bool_pack;
template <bool... v>
using all_true = std::is_same<bool_pack<true, v...>, bool_pack<v..., true>>;
namespace {
   template <bool... v>
   constexpr bool all_true_v = all_true<v...>::value;
}

Solution

  • consider

        bool const* g_b= &all_true_v<true>;
    

    this will have the same address in every translation unit for the inline constexpr version, but different adresses for the namespace {} version.