c ++类型别名在测试专业化时不起作用
c++ type alias not working when testing specialization
使用 C++,尝试实现:is_specialization_of
template<typename T, template<typename...> class Template>
struct is_specialization_of : std::false_type {};
template<template<typename...> class Template, typename... Tn>
struct is_specialization_of<Template<Tn...>, Template> : std::true_type {};
template<typename... Tn>
struct tstruct {};
template<typename... Tn>
using ustruct = tstruct<Tn...>;
int main( int argc, char **argv )
{
printf( "test u<int> against u, return %s\n", is_specialization_of<ustruct<int>, ustruct>::value ? "true" : "false" );
printf( "test u<int> against t, return %s\n", is_specialization_of<ustruct<int>, tstruct>::value ? "true" : "false" );
printf( "test t<int> against u return %s\n", is_specialization_of<tstruct<int>, ustruct>::value ? "true" : "false" );
printf( "test t<int> against t, return %s\n", is_specialization_of<tstruct<int>, tstruct>::value ? "true" : "false" );
getchar();
return 0;
}
Return:
test u<int> against u, return false
test u<int> against t, return true
test t<int> against u return false
test t<int> against t, return true
看起来类型别名与原始类型不完全一样
我正在使用 Visual Studio 社区 2017
Microsoft (R) C/C++ Optimizing Compiler Version 19.15.26732.1 for x64
然而,当尝试使用 gcc 编译相同的代码时,returns:
test u<int> against u, return true
test u<int> against t, return true
test t<int> against u return true
test t<int> against t, return true
有什么我可以做的解决方法吗?
Looks like the type alias is not considered exactly as the original type, or did I miss something?
别名专业化完全它代表的类型。但是别名 tempalte 是一个完全 不同的模板 。您输出的原因由 [temp.alias]
指定
1 A template-declaration in which the declaration is an
alias-declaration declares the identifier to be an alias template. An
alias template is a name for a family of types. The name of the alias
template is a template-name.
2 When a template-id refers to the specialization of an alias
template, it is equivalent to the associated type obtained by
substitution of its template-arguments for the template-parameters in
the type-id of the alias template.
如果我们在考虑以上两段的情况下检查您的测试用例,我们会看到:
"test u<int>
against u
" - ustruct<int>
等同于直接指定 tstruct<int>
。 tstruct<int>
不是 ustruct
的特化。该特征需要评估为 false。
"test u<int>
反对 t
" - 这里 ustruct<int>
再次等同于指定 tstruct<int>
直接地。 tstruct<int>
是 tstruct
的特化。该特征应报告为真。
"test t<int>
against u
" - tstruct<int>
不是 ustruct
的特化,就像我们之前观察到的那样。该特征应报告错误。
"test t<int>
against t
" - 应该报告为真。
当 运行 在 MSVC 中时,您的所有测试都符合 C++ 标准规定的它们应该执行的操作。 GCC 在这里不符合要求,这是一个编译器错误。
使用 C++,尝试实现:is_specialization_of
template<typename T, template<typename...> class Template>
struct is_specialization_of : std::false_type {};
template<template<typename...> class Template, typename... Tn>
struct is_specialization_of<Template<Tn...>, Template> : std::true_type {};
template<typename... Tn>
struct tstruct {};
template<typename... Tn>
using ustruct = tstruct<Tn...>;
int main( int argc, char **argv )
{
printf( "test u<int> against u, return %s\n", is_specialization_of<ustruct<int>, ustruct>::value ? "true" : "false" );
printf( "test u<int> against t, return %s\n", is_specialization_of<ustruct<int>, tstruct>::value ? "true" : "false" );
printf( "test t<int> against u return %s\n", is_specialization_of<tstruct<int>, ustruct>::value ? "true" : "false" );
printf( "test t<int> against t, return %s\n", is_specialization_of<tstruct<int>, tstruct>::value ? "true" : "false" );
getchar();
return 0;
}
Return:
test u<int> against u, return false
test u<int> against t, return true
test t<int> against u return false
test t<int> against t, return true
看起来类型别名与原始类型不完全一样
我正在使用 Visual Studio 社区 2017
Microsoft (R) C/C++ Optimizing Compiler Version 19.15.26732.1 for x64
然而,当尝试使用 gcc 编译相同的代码时,returns:
test u<int> against u, return true
test u<int> against t, return true
test t<int> against u return true
test t<int> against t, return true
有什么我可以做的解决方法吗?
Looks like the type alias is not considered exactly as the original type, or did I miss something?
别名专业化完全它代表的类型。但是别名 tempalte 是一个完全 不同的模板 。您输出的原因由 [temp.alias]
指定1 A template-declaration in which the declaration is an alias-declaration declares the identifier to be an alias template. An alias template is a name for a family of types. The name of the alias template is a template-name.
2 When a template-id refers to the specialization of an alias template, it is equivalent to the associated type obtained by substitution of its template-arguments for the template-parameters in the type-id of the alias template.
如果我们在考虑以上两段的情况下检查您的测试用例,我们会看到:
"test
u<int>
againstu
" -ustruct<int>
等同于直接指定tstruct<int>
。tstruct<int>
不是ustruct
的特化。该特征需要评估为 false。"
test u<int>
反对t
" - 这里ustruct<int>
再次等同于指定tstruct<int>
直接地。tstruct<int>
是tstruct
的特化。该特征应报告为真。"test
t<int>
againstu
" -tstruct<int>
不是ustruct
的特化,就像我们之前观察到的那样。该特征应报告错误。"test
t<int>
againstt
" - 应该报告为真。
当 运行 在 MSVC 中时,您的所有测试都符合 C++ 标准规定的它们应该执行的操作。 GCC 在这里不符合要求,这是一个编译器错误。