跟进为什么 cppreference 将 type_traits xxx_v 快捷方式定义为内联 constexpr 而不仅仅是 constexpr?,如果我创建自己的类型特征并希望避免 ODR 违规并希望它与 C++17 之前的项目兼容,是否将 xxx_v 快捷方式放在匿名命名空间中与显式声明它内联相同?
例如,all_true
从Check traits for all variadic template arguments中获取,使用 C++17,我可以在我的实用程序头中编写:
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;
这与编写以下与 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;
}