本文介绍了为什么在C ++ 11 / C ++ 14中有自动和支撑初始化器的特殊类型扣除规则?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Scott Meyers在他的CppCon 2014 talke 中提出了一个问题为什么在C ++ 11 / C ++ 14标准中有一个关于 auto 和支撑的初始化器的特殊规则(他的问题开始)。

auto与braced-init-在§7.1.6.4/ 6。






我想到了,也无法想出一个用例。到目前为止,我看到的最接近的事情是Bjarne Stroustrup使用它的一个例子。



在他的,他曾经使用 auto 捕获初始化器(但只是一个解决方法)。



这是代码(第30张幻灯片的一部分,中,希望禁止扣除平原类型通常来自支撑的初始化器列表的参数:

但是为 auto :创建了一个特殊的例外:

$ b $另一方面,对于
<$ c,能够推导出 initializer_list


In his CppCon 2014 talke "Type Deduction and Why You Care", Scott Meyers raises the question why there is a special rule about auto and braced initializers in the C++11/C++14 standard (his question starts at 36m05s).

The semantic of auto in combination with a braced-init-list is defined in §7.1.6.4/6.


I thought about it and could not come up with a use-case either. The closest thing that I have seen so far is one example where Bjarne Stroustrup used it.

In his Cpp 2014 talk "Make Simple Tasks Simple!", he once uses auto to capture initializers (but only as a workaround).

Here is the code (part of slide 30, at 37m10s):

    // auto ss1 = collect({ 1, 2, 3, 4, 5, 6 }, odd); // error: Bummer!
    auto lst = { 1, 2, 3, 4, 5, 6 };
    auto ss2 = collect(lst, odd);    // {1,3,5}

But note that it is only a workaround. He mentioned that it should not be necessary. Instead he would prefer to directly pass the arguments to the function. So, it cannot really serve as a good motivation for auto and initializer lists.


My understanding of C++ is not deep enough to judge the downsides of allowing initializer-lists in Bjarne's example, as he proposes. Anyway, it would avoid the need for auto in that case.

So, is auto and initializer list only a workaround for something that could have been better solved? Or are there good examples, where the extra auto deduction rule in §7.1.6.4/6 is useful?

解决方案

The rationale is in N2640, which wanted to ban deduction of a plain type parameter from a braced initializer list in general:

But carved out a special exception for auto:

这篇关于为什么在C ++ 11 / C ++ 14中有自动和支撑初始化器的特殊类型扣除规则?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 10:02