Trailing Return Types

Advantages of Trailing Return Types

Apart from template functions, what are some other advantages of using trailing return types in C++?

Abstract art representing computer programming

Trailing return types, while particularly beneficial in template functions, offer several other advantages that can enhance code readability and maintainability:

Improved Readability for Complex Return Types

For functions returning complex types, placing the return type after the parameter list can make the function signature clearer and easier to read. This is especially helpful when the return type involves templates or other intricate constructs. For example:

#include <iostream>
#include <memory>

template<typename T1, typename T2>
class MyClass {
 public:
  MyClass(T1 param1, T2 param2)
    : param1_(param1), param2_(param2) {}
  void Display() const {
    std::cout
      << "MyClass: param1 = " << param1_
      << ", param2 = " << param2_ << std::endl;
  }

 private:
  T1 param1_;
  T2 param2_;
};

auto CreateComplexObject(int param1, double param2)
  -> std::unique_ptr<MyClass<int, double>> {
  return std::make_unique<
    MyClass<int, double>>(param1, param2);
}

int main() {
  auto obj = CreateComplexObject(42, 3.14);
  obj->Display();
}
MyClass: param1 = 42, param2 = 3.14

Enhanced Consistency

Using trailing return types uniformly across a codebase can lead to more consistent and readable code. By always placing the return type after the function name and parameters, developers can maintain a predictable structure in their function declarations.

Emphasis on Function Name

Traditional function syntax places the return type before the function name, which can sometimes detract from quickly identifying what the function does. Trailing return types allow the function name to appear first, emphasizing the action performed by the function rather than its return type.

Alignment of Function Names

In classes with many member functions, trailing return types can help align the function names vertically. This alignment can make it easier to scan through a list of functions and quickly understand the class's interface.

#include <iostream>
#include <string>

class MyClass {
public:
auto GetValue() const -> int {
  return value_; }

auto SetValue(int newValue) -> void {
  value_ = newValue; }

auto GetName() const -> std::string {
  return name_; }

auto SetName(std::string newName) -> void {
  name_ = std::move(newName); }

private:
int value_ = 0;
std::string name_ = "Default";
}
;

int main() {
  MyClass obj;
  std::cout << "Initial Value: "
    << obj.GetValue() << std::endl;
  std::cout << "Initial Name: "
    << obj.GetName() << std::endl;

  obj.SetValue(100);
  obj.SetName("UpdatedName");

  std::cout << "Updated Value: "
    << obj.GetValue() << std::endl;
  std::cout << "Updated Name: "
    << obj.GetName() << std::endl;
}
Initial Value: 0
Initial Name: Default
Updated Value: 100
Updated Name: UpdatedName

While the use of trailing return types outside of template functions often comes down to personal preference and coding style, these benefits highlight their potential to improve the clarity and consistency of your code.

Some developers and codebases may prefer traditional return type syntax for its familiarity and simplicity, but trailing return types offer a viable alternative for those looking to enhance readability.

This Question is from the Lesson:

Trailing Return Types

An alternative syntax for defining function templates, which allows the return type to be based on their parameter types

Answers to questions are automatically generated and may not have been reviewed.

This Question is from the Lesson:

Trailing Return Types

An alternative syntax for defining function templates, which allows the return type to be based on their parameter types

A computer programmer
Part of the course:

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Free, unlimited access

This course includes:

  • 124 Lessons
  • 550+ Code Samples
  • 96% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved