Question

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?

Was it helpful?

Solution

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 is T1, and for a binary operator @ with a left operand of a type whose cv-unqualified version is T1 and a right operand of a type whose cv-unqualified version is T2, 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 of T1::operator@ (13.3.1.1.1); otherwise, the set of member candidates is empty.

(emphasis added by @sehe)

OTHER TIPS

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

just to re-iterate mine and Stephane's comment:

This is an obvious bug in clang, since your class obviously has an operator[](int).

Whether or not the code using decltype() is valid, is a subtly different question (I would say it is valid, but cannot prove it).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top