签到

05月05日
尚未签到

共有回帖数 0

    做月子

    等级:
    关于std::initalizer_list和auto
    The C++ Programming Language(Strostrup,4th edition,P165)有这种用法:
    auto x3{1,2,3};
    Visual Studio 2015 RC2下:
    auto i{2};
    //auto ii{2, 2};
    //error C3518: 'ii': in a direct-list-initialization
    //context the type for 'auto' can only be
    //deduced from a single initializer expression
    auto iii={2,2,2};
    typeid(i).name()得出结果为int
    typeid(iii).name()得出结果为class std::initalizer_listint
    Cppreference:
    In direct-list-initialization (but not in copy-list-initalization), when deducing the meaning of the auto from a braced-init-list, the braced-init-list must contain only one element, and the type of auto will be the type of that element:
    auto x1 = {3}; // x1 is std::initializer_listint
    auto x2{1, 2}; // error: not a single element
    auto x3{3}; // x3 is int (before C++17 it was std::initializer_listint)
    C++14标准文档暂时没找到解释。
    所以应该是书的问题吧,但是为什么就是不可以呢?auto不就是为了推断类型吗?希望各位有研究的吧友们指明(尤其是标准文档中的说明)。

    楼主 2016-06-16 11:34 回复

共有回帖数 0
  • 回 帖
  • 表情 图片 视频
  • 发表

登录直线网账号

Copyright © 2010~2015 直线网 版权所有,All Rights Reserved.沪ICP备10039589号 意见反馈 | 关于直线 | 版权声明 | 会员须知