Consider the following:
template<typename Der>
struct Base {
// NOTE: if I replace the decltype(...) below with auto, code compiles
decltype(&Der::operator()) getCallOperator() const {
return &Der::operator();
}
};
struct Foo : Base<Foo> {
double operator()(int, int) const {
return 0.0;
}
};
int main() {
Foo f;
auto callOp = f.getCallOperator();
}
I want to create a member function in CRTP base class with a return type depending on signature of the operator()
in the derived class. However decltype(&Der::operator())
fails to compile; The operator()
member function in Foo
is not visible. I assume that this is because the base class template is instantiated before Foo
is fully defined.
Surprisingly, if I place auto
for the return type it compiles. I assumed that auto
would make the compiler deduce the return type from the function body and fail - because the body uses the not fully defined Foo
type.
This behavior is the same for both MSVC 2015.3 and Clang 3.8
Why did the code start to work with auto
? Does auto
type deduction somehow "delay" the instantiation? Or use a different context than a hand-written return type expression?
question from:
https://stackoverflow.com/questions/38325730/why-does-auto-return-type-deduction-work-with-not-fully-defined-types 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…