encyclopegia.org

Trailing return type

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. 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. 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.

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. 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. 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. ==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 : using std::vector; class MyClass { private: vector items; public: bool hasMultipleItems() { return items.size() > 1; } }; A trailing return type is specified after the parameter list, following -> symbols: using std::vector; class MyClass { private: vector items; public: auto hasMultipleItems() -> bool { return items.size() > 1; } }; ==Distinction from other language features== In modern C++, the meaning of the auto keyword will depend on its context: When used in a variable's definition (e.g., auto x = 11; ), the auto keyword indicates type inference. The data type for that x will be deduced from its initialization. Starting with C++14, the return type of a function can also be inferred by using auto without specifying a trailing return type. For example: auto hasMultipleItems() { return items.size() > 1; } On the other hand, there is no type inference in the hasMultipleItems() example on the previous section. That example only uses the auto keyword as a syntactic element, because a trailing return type is being used. ==Rationale== Consider the task of programming a generic version of the following: 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) . However, 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 compile template decltype(lhs + rhs) add(const L& lhs, const R& rhs) { return lhs + rhs; } However, where a trailing return type is specified, the formal parameters are in scope: template auto add(const L& lhs, const R& rhs) -> decltype(lhs + rhs) { return lhs + rhs; } ==See also== Language enhancements of C++ 11 ==References==

References

No reference rows published for this article.

Cite this article

MLA

"Trailing return type." encyclopegia.org, 2026, https://encyclopegia.org/wiki/trailing-return-type. Accessed 24 November 2026.

Chicago

"Trailing return type." encyclopegia.org. Accessed November 24, 2026. https://encyclopegia.org/wiki/trailing-return-type.

APA

encyclopegia.org. (2026). Trailing return type. Retrieved November 24, 2026, from https://encyclopegia.org/wiki/trailing-return-type

Accessed November 24, 2026 · CC BY-SA 4.0 · citation page · JSON

Related articles

blood type

stub · references available

Type 69 tank

stub · references available

Return from the River Kwai

stub · references available

solar-type star

stub · references available

Adobe Type

stub · references available

Audi Type B

stub · references available

Audi Type G

stub · references available

Audi Type M

stub · references available
CC BY-SA 4.0 · share-alike · https://en.wikipedia.org/w/index.php?oldid=1371665598 · 0.5 · snapshot enwiki-001371665598

Cite · JSON · Search

Related services and records

Services

  • Estate — measured countries, lanes and facts
  • Intel — live corroborated events
  • Legal — statutes, cases and citations
  • Encyclopedia — articles with inline sources
  • Space — launches, satellites and orbits
  • Search — federated search and reports
  • Pass — passport account and SSO
  • Corpora — read-only corpus origins
  • e01 — self-hosted corpora and tools

Text and metadata only; each target carries its own sources. Missing data is published as no-data, never estimated.