Trailing Return Types

Trailing Return Types and Function Qualifiers

How do I use trailing return types with const, override, or final qualifiers?

Abstract art representing computer programming

When using trailing return types with function qualifiers like const, override, or final, the order of elements is important.

The const qualifier goes before the trailing return type:

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

But override and final go after the trailing return type:

class Base {
public:
  virtual auto GetValue() const -> int {
    return 10;
  }
};

class Derived : public Base {
public:
  auto GetValue() const -> int override {
    return 20;
  }
};

class MostDerived : public Derived {
public:
  auto GetValue() const -> int final {
    return 30;
  }
};

Remember:

  • const -> return type
  • return type -> override / final

This order can take some getting used to, especially since with traditional function syntax, all these qualifiers would go at the end. But with trailing return types, const moves to the beginning.

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:

  • 125 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