I am confused of the type of decltype a function. It is neither a function pointer nor functor. How can I use it? And why the full template specialization here does not require const qualifier.
class SomeClass
{
public:
template <typename T>
void insideTemplateMethod(const T & value)
{
}
};
template
void SomeClass::insideTemplateMethod<decltype(std::hex)>(decltype(std::hex) & ); // no need to specify const
template
void SomeClass::insideTemplateMethod<int>(int &); // error, must specify const
int main(void)
{}
if I remove the &, it then complains that
error: template-id 'insideTemplateMethod<std::ios_base&(std::ios_base&)>' for 'void SomeClass::insideTemplateMethod(std::ios_base& (*)(std::ios_base&))' does not match any template declaration"
Look, the decltype(std::hex) in parameter field is deducted to std::ios_base& (*)(std::ios_base&), whereas it is deducted to std::ios_base&(std::ios_base&) in the parameter of template.
Could you please help me understand it?
std::hexis a function, with the following declaration (see cppreference):If
Tis the type of this function, namelystd::ios_base&(std::ios_base&), then because it is a function type,const Tis the same asT. This is why the explicit instantiation definition can be written without theconst.Note that if you remove the
&from the explicit instantiation definition, then the function parameter typedecltype(std::hex)undergoes the standard transformation from a function type to a function pointer type. This is why you are seeing(*)in the error message.