Consider the code:
class Base{};
class Derived: public Base{};
template<Base& b> // references (and pointers) can be used as non-types
void f(){}
int main()
{
Derived d;
// f(d); // Error, template type must match exactly
f<(Base&)d>(); // Error here, why?!
}
I understand why the commented call fails: the template type must match exactly. I try however a cast in the second call, and get this error (gcc5.2):
error: 'd' is not a valid template argument for type 'Base&' because it is not an object with external linkage
Same error if I make Derived d;
global. clang is a bit more helpful, saying
... note: candidate template ignored: invalid explicitly-specified argument for template parameter 'b'
My question is: is the code above legal or not? If not, are there any reasons why?