Reputation: 3657
A template argument can be a constant expression (§C.5), the address of an object or function with external linkage (§9.2), or a non-overloaded pointer to member (§15.5). A pointer used as a template argument must be of the form
&ooff
, whereooff
is the name of an object or a function, or of the formff
, whereff
is the name of a function. A pointer to member must be of the form&XX::ooff
, whereooff
is the name of an member. In particular, a string literal is not acceptable as a template argument.
This is from stroustrup book. What's the logic behind what's allowed and what's not? Why string literal is not acceptable?
My understanding is that compiler should be able to deduce the type of the argument, so that it can generate code internally for that given type. Thanks for the help
Upvotes: 0
Views: 422
Reputation: 119877
The compiler does not deduce types of non-type template arguments. They are specified by the programmer. There's nothing to deduce.
The requirements are in place to ensure that the compiler can determine, at compile time, whether two pointers are equal. This, in turn, is needed to determine whether two uses of a class template (function template) refer to the same type (function).
Upvotes: 1