非静态数据成员初始化与新的表达式 [英] non-static data member initialization with new expression

查看:200
本文介绍了非静态数据成员初始化与新的表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下代码:

#include <map>

template <typename T>
struct X {
    std::map<int, T>* storage = new std::map<int, T>();
};

int main() {
    X<int> x;
}

这会在 clang 3.6.0 ,但无法在 gcc 5.1 。然而,如果 storage 的类型改为 std :: vector< T> * T * )。

This compiles on clang 3.6.0, but fails to compile on gcc 5.1. It would compile, however, if the type of storage were instead std::vector<T>* (or just T*).

我相当肯定这是gcc的编译器错误(编辑:我提交它 66344 ),但以为我会要求确保:是否有任何原因上述示例不应编译?

I'm fairly certain this is a compiler bug on gcc's part (edit: I submitted it as 66344), but thought I'd ask to make sure: is there any reason the above example shouldn't compile?

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    
           ^


推荐答案

这是中描述的问题的另一个示例核心问题325 (见2011年8月份的会议记录,它们有一个非常相似的例子),即模板参数列表中的逗号引起解析失败,当编译器尝试确定结束表达式是。

This is another example of the problem described in Core issue 325 (see the "Notes from the August, 2011 meeting" which have a very similar example), namely that the comma in the template argument list causes a parse failure when the compiler tries to determine where the end of the expression is.

问题仍然存在,但委员会的共识是它应该工作(我不知道会改变什么

The issue is still open, but the consensus of the committee is that it should be made to work (I don't know what will be changed to make it valid though).

Clang已经实现了一段时间的解决方法(可能暂时解析表达式并在失败时重试),Nathan Sidwell只有取消暂停相关的G ++错误并将其分配给自己,所以我希望他计划很快解决它。

Clang has implemented a workaround for some time (maybe tentatively parsing the expression and retrying if it fails) and Nathan Sidwell has just un-suspended the relevant G++ bug and assigned it to himself so I hope he plans to fix it soon.

这篇关于非静态数据成员初始化与新的表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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