C ++ 11:模板参数重新定义默认参数 [英] C++11: template parameter redefines default argument

查看:594
本文介绍了C ++ 11:模板参数重新定义默认参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用gcc编译以下源代码时,没有错误/警告:

When compiling the following source code with gcc there are no errors / warnings:

template< typename T = int > T func( );
template< typename T = int > T func( );



当我使用clang ++编译相同的源代码时,出现以下错误:

When I compile the same source code with clang++, I got the following error:

redeftempparam.cc:2:24: error: template parameter redefines default argument
template< typename T = int > T func( );
                       ^
redeftempparam.cc:1:24: note: previous default template argument defined here
template< typename T = int > T func( );
                       ^
1 error generated.

命令编译

[clang++|g++] -Wall -Werror -std=c++11 redeftempparam.cc


$ b b

(版本资讯:gcc 4.7.2,clang version 3.3(trunk 171722))

(Version information: gcc 4.7.2, clang version 3.3 (trunk 171722))

我的问题:

这种类型的重定义是允许的吗?

Is this type of redefinition allowed? If not: can you please point me to the appropriate point in the C++ standard?

推荐答案

§14.1.12:


模板参数不能在同一范围内由两个不同的声明给出默认参数。

A template-parameter shall not be given default arguments by two different declarations in the same scope.

[示例:

template<class T = int> class X;
template<class T = int> class X { /∗... ∗/ }; // error

- 结束示例]

这篇关于C ++ 11:模板参数重新定义默认参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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