Bar*_*rry 10 c++ gcc language-lawyer c++11
请考虑以下代码:
#include <map>
template <typename T>
struct X {
std::map<int, T>* storage = new std::map<int, T>();
};
int main() {
X<int> x;
}
Run Code Online (Sandbox Code Playgroud)
这在clang 3.6.0上编译,但无法在gcc 5.1上编译.但是,如果类型storage是std::vector<T>*(或只是T*),它将编译.
我相当肯定这是gcc上的一个编译器错误(编辑:我将其提交为66344),但我想要确认:上述例子是否有任何原因不能编译?
gcc编译错误:
main.cpp:5:51: error: expected ';' at end of member declaration
std::map<int, T>* storage = new std::map<int, T>();
^
main.cpp:5:51: error: declaration of 'std::map<int, T> X<T>::T'
main.cpp:3:11: error: shadows template parm 'class T'
template <typename T>
^
main.cpp:5:52: error: expected unqualified-id before '>' token
std::map<int, T>* storage = new std::map<int, T>();
^
main.cpp:5:46: error: wrong number of template arguments (1, should be at least 2)
std::map<int, T>* storage = new std::map<int, T>();
^
In file included from /usr/local/include/c++/5.1.0/map:61:0,
from main.cpp:1:
/usr/local/include/c++/5.1.0/bits/stl_map.h:96:11: note: provided for 'template<class _Key, class _Tp, class _Compare, class _Alloc> class std::map'
class map
^
Run Code Online (Sandbox Code Playgroud)
这是核心问题 325中描述的问题的另一个示例(请参阅“2011 年 8 月会议的注释”,其中有一个非常相似的示例),即当编译器尝试解析时,模板参数列表中的逗号会导致解析失败。确定表达式的结尾位置。
这个问题仍然悬而未决,但委员会的共识是应该让它发挥作用(尽管我不知道要改变什么才能使其有效)。
Clang 已经实施了一段时间的解决方法(也许是尝试性地解析表达式,如果失败则重试),而 Nathan Sidwell 刚刚取消了相关的 G++ bug 并将其分配给自己,所以我希望他计划尽快修复它。