UPDATE 2
Why is this marked a duplicate? Where and why do I have to put the “template” and “typename” keywords?
doesn't answer this question. The behavior described here isn't mentioned there anywhere (you won't find a single remark about the expected behavior of auto
there).
This is not a duplicate, especially since there's a conflicting behavior in different compilers.
UPDATE
Much like GCC, clang also fails to compile:
17 : error: expected expression
return p->is<true>();
^
However, on MSVC the code with the auto
detection is compiling successfully.
Clearly, there's a compiler bug somewhere.
Consider the following class:
struct A {
template<bool>
bool is() const {
return true;
}
template<bool>
static A* get() {
static A a;
return &a;
}
};
If a template function, such as
template<bool cond>
bool foo() {
auto p = A::get<cond>();
return p->is<true>();
}
tries to call A::is<bool>
, it fails miserably:
main.cpp: In function 'bool foo()':
main.cpp:17:24: error: expected primary-expression before ')' token
return p->is<true>();
^
main.cpp: In instantiation of 'bool foo() [with bool cond = true]':
main.cpp:21:22: required from here
main.cpp:17:17: error: invalid operands of types '<unresolved overloaded function type>' and 'bool' to binary 'operator<'
return p->is<true>();
^
however, if only we replace auto
with the explicit type (A*
):
template<bool cond>
bool foo() {
A* p = A::get<cond>();
return p->is<true>();
}
it works.
What is the reason for this?