为什么在C ++ 11 / C ++ 14中有自动和支撑初始化器的特殊类型扣除规则? [英] Why is there a special type deduction rule for auto and braced initializers in C++11/C++14?

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

问题描述

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

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






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



在他的 Cpp 2014谈使简单的任务简单!,他曾经使用 auto 捕获初始化器(但只是一个解决方法)。



这是代码(第30张幻灯片的一部分, 37m10s ):

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

但请注意,这只是一个解决方法。他提到,这不应该是必要的。相反,他更喜欢直接将参数传递给函数。因此,它不能真正成为 auto 和初始化程序列表的良好动机。






我对C ++的理解不够深刻,不足以判断Bjarne例子中允许初始化列表的缺点,正如他的建议。无论如何,这将避免在 auto 的需要。



所以,是 auto 和初始化列表只有一个解决方法,可以更好地解决的事情吗?或者有很好的例子,§7.1.6.4/ 6中的额外自动扣除规则是有用的?

解决方案

N2640 中,希望禁止扣除平原类型通常来自支撑的初始化器列表的参数:


  template< class T& 
void inc(T,int); //(1)

template< class T>
void inc(std :: initializer_list< T> long); //(2)

inc({1,2,3},3); // Calls(2)。 (如果扣除成功
//为(1),(1)将被称为 - a
//惊喜。)


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

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

$ c> T 是有吸引力的允许:

  auto x = {1,1,2, 3,5}; 
f(x);
g(x);


EWG讨论初始化列表的开始就被认为是可取的行为。而不是为一个参数类型 T 与{} -list
匹配的一个
聪明的扣除规则(我们在之前的草图和本草案),
我们现在更喜欢处理这个与auto变量
扣除当初始化器是{} - 列表时的特殊情况。也就是说,对于用auto类型说明符和
{} -list初始化函数声明的变量的特定
情况,auto被推导为函数
f(initializer_list 而不是对于 f(T)



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:

template<class T>
void inc(T, int); // (1)

template<class T>
void inc(std::initializer_list<T>, long); // (2)

inc({1, 2, 3}, 3); // Calls (2). (If deduction had succeeded
                   // for (1), (1) would have been called — a
                   // surprise.)

But carved out a special exception for auto:

On the other hand, being able to deduce an initializer_list<X> for T is attractive to allow:

auto x = { 1, 1, 2, 3, 5 };
f(x);
g(x);

which was deemed desirable behavior since the very beginning of the EWG discussions about initializer lists. Rather than coming up with a clever deduction rule for a parameter type T matched with a {}-list (an option we pursued in earlier sketches and drafts of this paper), we now prefer to handle this with a special case for "auto" variable deduction when the initializer is a {}-list. I.e., for the specific case of a variable declared with an "auto" type specifier and a {}-list initializer, the "auto" is deduced as for a function f(initializer_list<T>) instead of as for a function f(T).

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

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