【发布时间】:2018-11-25 10:57:39
【问题描述】:
使用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;
}
返回:
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 Community 2017
Microsoft (R) C/C++ 优化编译器版本 19.15.26732.1 for x64
但是,当尝试使用 gcc 编译相同的代码时,它会返回:
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
有什么办法可以解决吗?
【问题讨论】:
标签: c++ templates specialization type-alias