简体   繁体   English

在C ++ 14中使用auto作为返回和参数类型

[英]Use of auto as return and parameters type in C++14

In the 4th edition of Bjarne Stroustrup book (The C++ programing language) we read that: 在Bjarne Stroustrup第4版(C ++编程语言)中,我们读到:

Using auto , we avoid redundancy and writing long type names. 使用auto,我们可以避免冗余并编写长类型名称。 This is especially important in generic programming where the exact type of an object can be hard for the programmer to know and the type names can be quite long (§4.5.1). 这在通用编程中尤为重要,因为对象的确切类型可能很难让程序员知道,类型名称可能很长(§4.5.1)。

So, to understand the importance of this type. 所以,要了解这种类型的重要性。 I made this small test program: 我做了这个小测试程序:

#include <iostream>

/*-----------------------------*/
auto multiplication(auto a, auto b)
{
    return a * b;
}


int main()
{
  auto c = multiplication(5,.134);
  auto d = 5 * .134;
  std::cout<<c<<"\n"<<d<<"\n";

}

The stdout of this program (compiled with -std=C++14): 这个程序的标准输出(用-std = C ++ 14编译):

0
0.67

I am wondering why I got different results (types) with c and d variables even if the return type of multiplication function is auto. 我想知道为什么我用c和d变量得到不同的结果(类型),即使乘法函数的返回类型是auto。

EDIT: My GCC version: gcc version 5.4.0 20160609 编辑:我的GCC版本: gcc version 5.4.0 20160609

To start with, your code makes use of gcc extension , namely auto function parameters . 首先,您的代码使用gcc扩展名 ,即自动函数参数

I guess your gcc version does not work with the extension properly and provides an incorrect result (with gcc 7.1 I have 0.67 0.67 even using auto parameters). 我猜你的gcc版本无法正常使用扩展并提供不正确的结果(使用gcc 7.1我甚至使用自动参数也有0.67 0.67 )。

The normal way to rewrite your function in a standard C++ is to apply templates: 在标准C ++中重写函数的常规方法是应用模板:

template<typename T, typename U>
auto multiplication(T a, U b)
{
    return a * b;
}

and let the compiler to deduce return type. 并让编译器推导出返回类型。

To start with Bjarne Stroustrup in his 4th edition of his seminal book "The C++ Programming Language" doesn't refer to the use of auto as rendered in your code example. 首先是Bjarne Stroustrup在他的开创性着作“The C ++ Programming Language”的第4版中,并未提及在代码示例中使用auto But rather to the standard use of the auto specifier as: 而是auto说明符的标准用法:

  • Specifier for variables where their type is going to be deduced by its initializer (eg, auto i = 0; ). 变量的说明符,其类型将由其初始化程序推导出来(例如, auto i = 0; )。
  • Specifier for function's return type where it's going to be deduced by its trailing return type or from its return statement. 函数返回类型的说明符,它将由其尾随返回类型或其返回语句推导出。

    auto foo(int a, int b) {return a + b; }

In your example you're referring to the use of auto as a placeholder as suggested by C++ extensions for Concepts (N4674) proposal. 在您的示例中,您指的是使用auto作为占位符,如C ++扩展概念(N4674)提议所示。 Unfortunately, this is not standard C++ yet. 不幸的是,这还不是标准的C ++。 It was to be accepted in C++17 but it didn't make it. 它在C ++ 17中被接受,但它没有成功。 Hopes now rise for C++20. 对C ++ 20的希望现在上升了。 However, use of auto like that is provided by GCC as an extension . 但是,GCC提供的类似auto使用作为扩展 Work on C++ concepts for GCC started very early, it even predates the advent of C++11. 关于GCC的C ++概念的工作很早就开始了,它甚至早于C ++ 11的出现。 At some point work on concepts was abandoned and then restarted under another name namely Concepts Lite. 在某些时候,关于概念的工作被放弃,然后以另一个名称Concept Lite重新启动。 Support back then was pretty unstable (eg, GCC version 5.4). 当时的支持非常不稳定(例如,GCC版本5.4)。 Thus, what you're experiencing is a GCC bug. 因此,您遇到的是GCC错误。 In more recent versions of GCC this bug has been corrected. 在更新版本的GCC中,此错误已得到纠正。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM