C ++ 11模板别名作为模板模板参数导致不同类型? [英] C++11 template alias as template template argument leads to different type?

查看:310
本文介绍了C ++ 11模板别名作为模板模板参数导致不同类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在编译下列源代码时发现了一个奇怪的行为:

  template< template< class>类TT> struct X {}; 
模板< class> struct Y {};
template< class T>使用Z = Y T;

int main(){
X< Y> y;
X< Z> z;
z = y; // it failed here
}

这是一个稍微修改的例子,取自c + +11模板别名的标准提案: http://www.open- std.org/jtc1/sc22/wg21/docs/papers/2007/n2258.pdf (请参阅第4页)
另请注意,提案将y和z声明为相同类型。在我们的解释中,因此应该可以从y分配(或复制构造)z。



但是,此代码不能用gcc 4.8.1编译, 3.3。这是编译器中的错误还是我们误解标准吗?



先感谢,
craffael et al;)



PS Clang错误消息是:

 错误:无可重载'='

注意:候选函数(隐式副本赋值运算符)不可行:对于第一个参数,从'X< template Y>'到'const X< template Z>'的已知转换
template< template&类TT> struct X {};

注意:候选函数(隐式移动赋值运算符)不可行:对于第一个参数,从'X< template Y>'到'X< template Z>'的已知转换
template< template< class>类TT> struct X {};


解决方案

目前的标准并没有这么说,意图是y和z具有相同的类型。有一个开放的核心工作组问题: http://wg21.cmeerw.net/cwg/issue1286


We have observed a strange behaviour in the compilation of the follwing source code:

template<template<class> class TT> struct X { };
template<class> struct Y { };
template<class T> using Z = Y<T>;

int main() {
  X<Y> y;
  X<Z> z;
  z = y; // it fails here
}

This is a slightly modified example taken from the c++11 standard proposal for template aliases: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2258.pdf (See page 4) Also note that the proposal "declares y and z to be of the same type." In our interpretation it should therefore be possible to assign (or copy construct) z from y.

However, this code doesn't compile with gcc 4.8.1 nor with clang 3.3. Is this an error in the compiler or did we misunderstand the standard?

Thanks in advance, craffael et al ;)

P.S. The Clang error message is:

error: no viable overloaded '='

note: candidate function (the implicit copy assignment operator) not viable: no known conversion from 'X<template Y>' to 'const X<template Z>' for 1st argument
template<template<class> class TT> struct X { };

note: candidate function (the implicit move assignment operator) not viable: no known conversion from 'X<template Y>' to 'X<template Z>' for 1st argument
template<template<class> class TT> struct X { };

解决方案

The current standard doesn't say so, but the intention is that y and z have the same type. There is an open Core Working Group issue for this: http://wg21.cmeerw.net/cwg/issue1286

这篇关于C ++ 11模板别名作为模板模板参数导致不同类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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