I was trying to reuse the return type of an operator in my trailing return type of another function, but unfortunately clang did not accept it
struct A {
int operator[](int);
auto at(int i) -> decltype((*this)[i]);
};
Clang says that my class does not have a operator[]. Gcc did accept my code. Is my code actually invalid?
The trailing return type feature removes a C++ limitation where the return type of a function template cannot be generalized if the return type depends on the types of the function arguments.
In C++14, you can just use auto as a return type.
decltype(auto) is primarily useful for deducing the return type of forwarding functions and similar wrappers, where you want the type to exactly “track” some expression you're invoking.
I would say that clang is correct, due to 13.3.1.2p3 (1st bullet).
For a unary operator
@
with an operand of a type whose cv-unqualified version isT1
, and for a binary operator@
with a left operand of a type whose cv-unqualified version isT1
and a right operand of a type whose cv-unqualified version isT2
, three sets of candidate functions, designated member candidates, nonmember candidates and built-in candidates, are constructed as follows:
- If
T1
is a complete class type, the set of member candidates is the result of the qualified lookup ofT1::operator@
(13.3.1.1.1); otherwise, the set of member candidates is empty.
(emphasis added by @sehe)
It seems, that it's CLang's bug, because the next code
struct A {
int operator[](int);
auto at(int i) -> decltype( this-> operator[]( i ) );
};
compiles by CLang as well - http://liveworkspace.org/code/2Myghk$6
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With