模板别名的相等性
Equality of template aliases
我尝试创建无法与原始模板区分开来的模板别名。
因此,我创建特征来检查 2 个模板(不是类型)何时相等:
template <template <class...> class C1,
template <class...> class C2>
struct is_same_template : std::false_type {};
template <template <class...> class C1>
struct is_same_template<C1, C1> : std::true_type {};
现在测试一下:
// Expected alias
template <typename ... Ts> using V_Ts = std::vector<Ts...>; // Variadic
// Fallback alias
template <typename T, typename A> using V = std::vector<T, A>; // Exact count
static_assert(!is_same_template<std::vector, V_Ts>::value); // Alias rejected by gcc/clang
static_assert( is_same_template<std::vector, V>::value); // Alias accepted only for gcc
是否可以创建 "true" 别名?
哪个编译器是正确的?
I try to create template alias which cannot be distinguished from original.
我认为目前这不可能。 (不幸的是)没有模板别名,只有别名模板。别名模板始终是它自己的模板 [temp.alias]/1. A specialization of an alias template is equivalent to the type you get by substituting the template arguments into the alias template, but the alias template itself is not an alias for another template [temp.alias]/2。我会考虑让 GCC 让你的第二个 static_assert
通过 GCC 中的错误…
正如@HolyBlackCat 在上面的评论中指出的那样, which points to numerous related CWG issues. One issue in particular (CWG 1286) would seem to suggest that there is desire to allow an alias template to itself be equivalent to the template it refers to under certain circumstances. However, it does not seem that the proposed resolution has been adopted due to concerns raised later. The relevant wording in the current standard draft ([temp.alias] and [temp.type]) 似乎与 C++11 没有变化……
我尝试创建无法与原始模板区分开来的模板别名。
因此,我创建特征来检查 2 个模板(不是类型)何时相等:
template <template <class...> class C1,
template <class...> class C2>
struct is_same_template : std::false_type {};
template <template <class...> class C1>
struct is_same_template<C1, C1> : std::true_type {};
现在测试一下:
// Expected alias
template <typename ... Ts> using V_Ts = std::vector<Ts...>; // Variadic
// Fallback alias
template <typename T, typename A> using V = std::vector<T, A>; // Exact count
static_assert(!is_same_template<std::vector, V_Ts>::value); // Alias rejected by gcc/clang
static_assert( is_same_template<std::vector, V>::value); // Alias accepted only for gcc
是否可以创建 "true" 别名? 哪个编译器是正确的?
I try to create template alias which cannot be distinguished from original.
我认为目前这不可能。 (不幸的是)没有模板别名,只有别名模板。别名模板始终是它自己的模板 [temp.alias]/1. A specialization of an alias template is equivalent to the type you get by substituting the template arguments into the alias template, but the alias template itself is not an alias for another template [temp.alias]/2。我会考虑让 GCC 让你的第二个 static_assert
通过 GCC 中的错误…
正如@HolyBlackCat 在上面的评论中指出的那样,