Home >Backend Development >C++ >Should Trailing Return Types Be the Standard in New C 11 Code?

Should Trailing Return Types Be the Standard in New C 11 Code?

Patricia Arquette
Patricia ArquetteOriginal
2024-12-09 12:52:11192browse

Should Trailing Return Types Be the Standard in New C  11 Code?

Trailing Return Type Syntax: Should It Be the Norm in New C 11 Programs?

C 11 introduced a new function syntax that allows specifying the return type at the end of the parameter list:

auto func_name(int x, int y) -> int;

While this syntax is supported, its adoption has been limited. The question arises whether it should be the default style in new C 11 programs.

Where Trailing Return Types Are Required

Certain situations necessitate the use of trailing return types, such as:

  • Lambda return types
  • Return types that depend on argument names in decltype

Advantages of Trailing Return Types

Beyond these requirements, trailing return types offer several advantages:

  • Reduced Repetition: For member functions that use member typedefs, trailing return types eliminate the need to repeat the class name, improving readability.
  • Alignment of Function Names: Using trailing return types consistently aligns the names of all functions, making code easier to scan.

Conclusion

While trailing return types have certain advantages, they are not currently widely adopted. However, their use in specific cases and their potential for enhancing readability suggest that they may become more prevalent in the future. For new C 11 programs, it is recommended to consider adopting trailing return types where appropriate to improve code aesthetics and maintain consistency.

The above is the detailed content of Should Trailing Return Types Be the Standard in New C 11 Code?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn