is_same_template对模板别名的奇怪行为 [英] Strange behaviour of is_same_template on template aliases

查看:140
本文介绍了is_same_template对模板别名的奇怪行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下程序...

  #include< iostream> 
#include< type_traits>

template< typename T>
struct Template {};

template< typename T>
using Alias = Template< T> ;;

模板
<
template< typename>
T1类,
模板< typename>
class T2
>
struct is_same_template:std :: false_type {};

模板
<
template< typename>
class T
>
struct is_same_template< T,T> :std :: true_type {};

int main(){
std :: cout< std :: boolalpha;
std :: cout<< Template == Template:<< is_same_template< Template,Template> :: value<< std :: endl;
std :: cout<< Template == Alias:<< is_same_template< Template,Alias> :: value<< std :: endl;
std :: cout<< Alias == Alias:< is_same_template< Alias,Alias> :: value<< std :: endl;
}

... outputs ...

 模板==模板:true 
模板==别名:false
别名==别名:true
g ++ 4.8.1 , 3.4 和编译的

vc ++ 18.00.21005.1



是这些编译器的错误还是标准的要求?

解决方案

这是标准所要求的行为,它是完全合乎逻辑的。别名模板不是模板别名(尽管有人想要)。最初,即使在标准中也出现了一些混乱,请参阅 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1244



在当前标准化的表单中,别名模板就像它的非模板化计数器部分:它别名一个类型。在模板版本中,类型可能是依赖的。



并立即替代。例如其本身是模板参数的 c>将是依赖类型模板< T> - 在这个意义上,名称​​别名模板可能有点令人困惑,因为它建议在某个时刻实例化别名声明。但实际上立即取代别名模式 - 在这个意义上,模板版本更像是一个始终存在且不需要实例化的依赖别名声明,而不是一个别名声明模板。



虽然如此,这就是一种哲学。


The following program...

#include <iostream>
#include <type_traits>

template <typename T>
struct Template{};

template <typename T>
using Alias = Template<T>;

template
    <
        template <typename>
        class T1,
        template <typename>
        class T2
    >
struct is_same_template : std::false_type{};

template
    <
        template <typename>
        class T
    >
struct is_same_template<T, T> : std::true_type{};

int main() {
    std::cout << std::boolalpha;
    std::cout << "Template == Template: " << is_same_template<Template, Template>::value << std::endl;
    std::cout << "Template == Alias: " << is_same_template<Template, Alias>::value << std::endl;
    std::cout << "Alias == Alias: " << is_same_template<Alias, Alias>::value << std::endl;
}

...outputs...

Template == Template: true
Template == Alias: false
Alias == Alias: true

...compiled with g++ 4.8.1, clang 3.4 and vc++ 18.00.21005.1.

Is it a bug in these compilers or a requirement of the standard?

解决方案

That is the behavior required by the Standard and it's perfectly logical. A alias template is not a template alias (despite intended by some to be). Initially there appears to have been some confusion even in the Standard about this, see http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1244 .

In the currently Standardized form, an alias template is like its non-templated counter part: It aliases a type. In the template version, the type may be dependent.

And it is immediately substituted. For example Alias<T> with T being itself a template parameter will be the dependent type Template<T> - in this sense the name alias template might be a bit confusing, because it suggests that there will be alias declaration instantiated at some point. But actually the alias pattern is substitued immediately - in this sense the templated version is more like a dependent alias declaration that always exists and does not need to be instantiated, rather than being an alias declaration template.

On that end, it becomes a bit philosophical what precisely we mean with those terms, though.

这篇关于is_same_template对模板别名的奇怪行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆