Trailing return type

Last updated

In computer programming, a subroutine (a.k.a. function) will often inform calling code about the result of its computation, by returning a value to that calling code. The data type of that value is called the function's return type.

Contents

In the C++ programming language, a function must be declared. The C++ function's return type is specified as a part of declaring that function. [1] A trailing return type, a syntax feature available since C++11, is like a traditional return type, except that it is specified in a different location. [2] [3] [4]

Syntax

An ordinary return type is specified before the function's name. In this example of traditional C++ code, the return type of HasMultipleItems() is bool:

classCClass{public:boolHasMultipleItems();std::vector<int>m_veciMember;};boolCClass::HasMultipleItems(){returnm_veciMember.size()>1;}

A trailing return type is specified after the parameter list, following -> symbols:

classCClass{public:autoHasMultipleItems()->bool;std::vector<int>m_veciMember;};autoCClass::HasMultipleItems()->bool{returnm_veciMember.size()>1;}

Distinction from other language features

In modern C++, the meaning of the auto keyword will depend on its context:

Rationale

Consider the task of programming a generic version of int Add(const int& lhs, const int& rhs) { return lhs + rhs; }. A proper expression of this function's return type would use the two formal parameter names with decltype: decltype(lhs + rhs). But, where a return type is traditionally specified, those two formal parameters are not yet in scope. Consequently, this code will not compile:

// This will not compiletemplate<typenameTL,typenameTR>decltype(lhs+rhs)Add(constTL&lhs,constTR&rhs){returnlhs+rhs;}

The formal parameters are in scope, where a trailing return type is specified:

template<typenameTL,typenameTR>autoAdd(constTL&lhs,constTR&rhs)->decltype(lhs+rhs){returnlhs+rhs;}

See also

Related Research Articles

In computer programming, operator overloading, sometimes termed operator ad hoc polymorphism, is a specific case of polymorphism, where different operators have different implementations depending on their arguments. Operator overloading is generally defined by a programming language, a programmer, or both.

Templates are a feature of the C++ programming language that allows functions and classes to operate with generic types. This allows a function or class declaration to reference via a generic variable another different class without creating full declaration for each of these different classes.

Template metaprogramming (TMP) is a metaprogramming technique in which templates are used by a compiler to generate temporary source code, which is merged by the compiler with the rest of the source code and then compiled. The output of these templates can include compile-time constants, data structures, and complete functions. The use of templates can be thought of as compile-time polymorphism. The technique is used by a number of languages, the best-known being C++, but also Curl, D, Nim, and XL.

In mathematics and computer science, a higher-order function (HOF) is a function that does at least one of the following:

In computer programming, a function object is a construct allowing an object to be invoked or called as if it were an ordinary function, usually with the same syntax. In some languages, particularly C++, function objects are often called functors.

In the C++ programming language, a reference is a simple reference datatype that is less powerful but safer than the pointer type inherited from C. The name C++ reference may cause confusion, as in computer science a reference is a general concept datatype, with pointers and C++ references being specific reference datatype implementations. The definition of a reference in C++ is such that it does not need to exist. It can be implemented as a new name for an existing object.

typedef is a reserved keyword in the programming languages C, C++, and Objective-C. It is used to create an additional name (alias) for another data type, but does not create a new type, except in the obscure case of a qualified typedef of an array type where the typedef qualifiers are transferred to the array element type. As such, it is often used to simplify the syntax of declaring complex data structures consisting of struct and union types, although it is also commonly used to provide specific descriptive type names for integer data types of varying sizes.

A class in C++ is a user-defined type or data structure declared with keyword class that has data and functions as its members whose access is governed by the three access specifiers private, protected or public. By default access to members of a C++ class is private. The private members are not accessible outside the class; they can be accessed only through methods of the class. The public members form an interface to the class and are accessible outside the class.

The C and C++ programming languages are closely related but have many significant differences. C++ began as a fork of an early, pre-standardized C, and was designed to be mostly source-and-link compatible with C compilers of the time. Due to this, development tools for the two languages are often integrated into a single product, with the programmer able to specify C or C++ as their source language.

C++11 is a version of the ISO/IEC 14882 standard for the C++ programming language. C++11 replaced the prior version of the C++ standard, called C++03, and was later replaced by C++14. The name follows the tradition of naming language versions by the publication year of the specification, though it was formerly named C++0x because it was expected to be published before 2010.

A property, in some object-oriented programming languages, is a special sort of class member, intermediate in functionality between a field and a method. The syntax for reading and writing of properties is like for fields, but property reads and writes are (usually) translated to 'getter' and 'setter' method calls. The field-like syntax is easier to read and write than many method calls, yet the interposition of method calls "under the hood" allows for data validation, active updating, or implementation of what may be called "read-only fields".

In computer programming, an anonymous function is a function definition that is not bound to an identifier. Anonymous functions are often arguments being passed to higher-order functions or used for constructing the result of a higher-order function that needs to return a function. If the function is only used once, or a limited number of times, an anonymous function may be syntactically lighter than using a named function. Anonymous functions are ubiquitous in functional programming languages and other languages with first-class functions, where they fulfil the same role for the function type as literals do for other data types.

Expression templates are a C++ template metaprogramming technique that builds structures representing a computation at compile time, where expressions are evaluated only as needed to produce efficient code for the entire computation. Expression templates thus allow programmers to bypass the normal order of evaluation of the C++ language and achieve optimizations such as loop fusion.

In computer programming, variadic templates are templates that take a variable number of arguments.

Concepts are an extension to the templates feature provided by the C++ programming language. Concepts are named Boolean predicates on template parameters, evaluated at compile time. A concept may be associated with a template, in which case it serves as a constraint: it limits the set of arguments that are accepted as template parameters.

In the C++ programming language, decltype is a keyword used to query the type of an expression. Introduced in C++11, its primary intended use is in generic programming, where it is often difficult, or even impossible, to express types that depend on template parameters.

"typename" is a keyword in the C++ programming language used when writing templates. It is used for specifying that a dependent name in a template definition or declaration is a type. In the original C++ compilers before the first ISO standard was completed, the typename keyword was not part of the C++ language and Bjarne Stroustrup used the class keyword for template arguments instead. While typename is now the preferred keyword, older source code may still use the class keyword instead.

In the context of the programming language C++, functional refers to a header file that is part of the C++ Standard Library and provides a set of predefined class templates for function objects, including operations for arithmetic, comparisons, and logic. Instances of these class templates are C++ classes that define a function call operator, and the instances of these classes can be called as if they were functions. It is possible to perform very sophisticated operations without writing a new function object, simply by combining predefined function objects and function object adaptors.

C++14 is a version of the ISO/IEC 14882 standard for the C++ programming language. It is intended to be a small extension over C++11, featuring mainly bug fixes and small improvements, and was replaced by C++17. Its approval was announced on August 18, 2014. C++14 was published as ISO/IEC 14882:2014 in December 2014.

C23 is the informal name for what will likely become ISO/IEC 9899:2024, the next standard for the C programming language, which will replace C17. It was started in 2016 informally as C2x, and expected to be published in 2024. The most recent publicly available working draft of C23 was released on April 1, 2023. The first WG14 meeting for the C2x draft was held in October 2019, virtual remote meetings were held in 2020 due to the COVID-19 pandemic, then various teleconference meetings continued to occur through 2023.

References

  1. Stroustrup, Bjarne (2013). The C++ Programming Language (Fourth ed.). Addison-Wesley. ISBN   978-0-321-56384-2.
  2. "Function declaration". cppreference.com. C++ Reference. Retrieved 1 March 2021.
  3. "C++0x Suffix Return Types". cplusplus.com. The C++ Resources Network. Retrieved 1 March 2021.
  4. "Functions (C++)". Microsoft C++, C, and Assembler. Microsoft Corporation. Retrieved 1 March 2021.