[英]Valid C++03 template code won't compile in C++11
I have come across a small (easily solvable though) problem while writing valid C++03 template code, which compiles normally, that will not compile when using the C++11 dialect. 在编写正常编译的有效C ++ 03模板代码时,我遇到了一个小的(很容易解决的)问题,在使用C ++ 11方言时无法编译。
The problem arises at the template parameter resolution. 问题出现在模板参数分辨率上。 Let this code be an example of this:
让这段代码成为一个例子:
template <uint32_t number>
struct number_of_bits {
enum {
value = 1 + number_of_bits<number >> 1>::value
};
};
template <>
struct number_of_bits<0> {
enum {
value = 0
};
};
Since C++11 allows now ">>" to finish a template parameter list that takes a templated parameter as the last argument, it creates a problem when parsing this code. 由于C ++ 11现在允许“>>”完成一个模板参数列表,该列表将模板化参数作为最后一个参数,因此在解析此代码时会产生问题。
I am using GCC (version 4.8.1) as my compiler, and it compiles normally using the command line: 我使用GCC(版本4.8.1)作为我的编译器,它使用命令行正常编译:
g++ test.cc -o test
But it fails to compile when I add the -std=c++11
command line switch: 但是当我添加
-std=c++11
命令行开关时它无法编译:
g++ -std=c++11 test.cc -o test
Is this a C++11 language feature or is it a bug in GCC? 这是一个C ++ 11语言功能还是GCC中的一个错误? is this a known bug if it's the case?
这是一个已知的错误,如果是这样的话?
Clang++ gives me a warning in -std=c++03
mode: Clang ++在
-std=c++03
模式中给出了警告:
test.cpp:6:43: warning: use of right-shift operator ('>>') in template argument
will require parentheses in C++11 [-Wc++11-compat]
value = 1 + number_of_bits<number >> 1>::value
^
( )
And indeed, in C++11 the parsing rules were revised so that >>
always closes template parameters in template context. 实际上,在C ++ 11中,修改了解析规则,以便
>>
始终在模板上下文中关闭模板参数。 As the warning notes, you should just put parens around the parameter to fix the parsing issue: 正如警告所述,您应该在参数周围放置一些parens来修复解析问题:
value = 1 + number_of_bits<(number >> 1)>::value
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.