【发布时间】:2018-01-03 13:02:00
【问题描述】:
跟进Why does cppreference define type_traits xxx_v shortcuts as inline constexpr and not just constexpr?,如果我创建自己的类型特征并希望避免 ODR 违规并希望它与 C++17 之前的项目兼容,则将 xxx_v 快捷方式放在匿名命名空间中,与明确声明它内联?
例如,从Check traits for all variadic template arguments 中获取all_true,使用 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;
}
【问题讨论】:
-
未命名的命名空间提供了内部链接中的所有内容...并且
all_true_v无论如何都有内部链接,原因与@Oliv 在您的另一个问题中引用的原因相同(C++ 14 中的相同项目符号)。所以你提供的两个代码 sn-ps 根本不兼容。 -
@StoryTeller 当然。我的混搭。
标签: c++ c++17 inline-variable