Trailing Return Types and Function Qualifiers
How do I use trailing return types with const, override, or final qualifiers?
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.
Trailing Return Types
An alternative syntax for defining function templates, which allows the return type to be based on their parameter types