简体   繁体   中英

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;
}

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.

You do avoid the ODR violation with the anonymous namespace, as it creates a new separate set of objects in each file where it is included. The advantage of an inline object is that there will only be one in total.

However, if you only use the constexpr values as constants, you will not notice much of a difference. And a nice compiler might avoid storing the constants in the data area anyway.

Passing references or pointers around and comparing addresses could make a difference, like Tobi says. But perhaps you can avoid comparing the addresses of two constant values?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM