这个问题在这里已有答案:
表达式的不同编译器行为:auto p {make_pointer()}; 1个答案当我用clang和gcc编译下面的代码时, T的推导方式不同。
#include<initializer_list> //for clang //to see how T is deduced(form compiler error). template<typename T> void foo(T); int main() { auto var1{2}; foo(var1); }这是我得到的。
clang 3.6(c++11/c++14) gcc 4.9(c++11/c++14) T = std::initializer_list<int> gcc 5.1(c++11/c++14) T = int我认为T应该是std::initializer_list<int> 。
为什么在gcc 5.1中T = int ?
This question already has an answer here:
Different compiler behavior for expression: auto p {make_pointer()}; 1 answerWhen I compile the below code with clang and gcc T is deduced differently.
#include<initializer_list> //for clang //to see how T is deduced(form compiler error). template<typename T> void foo(T); int main() { auto var1{2}; foo(var1); }Here is what I got.
clang 3.6(c++11/c++14) gcc 4.9(c++11/c++14) T = std::initializer_list<int> gcc 5.1(c++11/c++14) T = intI think T should be std::initializer_list<int>.
Why is T = int in gcc 5.1?
最满意答案
这是对C ++ 17规范的改进建议 - N3922 (我不确定它是否已被接受)。
基本上来自Scott Meyers的演示文稿 ,幻灯片20涵盖了新规则。
auto var1 {2} ;这里, var1将被推断为int 。
看起来好像有些编译器已经实现了这一改变。 我相信这种变化更“直观”,但你的里程可能会有所不同。 我认为在这个临时阶段,更喜欢=初始化,它可能更便携。
这里的答案更详细地说明了提案的历史和提出的缺陷。
This is proposed change to the C++17 specification - N3922 (I'm not sure if it has been accepted yet).
Basically this presentation from Scott Meyers, slide 20 covers the new rules.
auto var1 {2} ;Here, var1 will be deduced to be an int.
It does look like some compilers have already implemented the change. I believe the change is more "intuitive" but your mileage may vary. I think in this interim phase, prefer the = initialisation, it may be more portable.
The answer here has some more detail on the history of the proposals and defects raised.
更多推荐
发布评论