Re: default value for pointer in templates
"Mycroft Holmes" <m.holmes@nospam.it> wrote in message
news:%23H1S4PJbIHA.2268@TK2MSFTNGP02.phx.gbl
I know that some casts are illegal in template parameters, but
assigning zero to a pointer says "invalid conversion from int to
pointer"... it looks a bit pedantic :)
this does not work:
template <typename T, int (*F)(int) = 0>
class XXX
{
};
14.3.2/5 ...
- for a non-type template-parameter of type pointer to object,
qualification conversions (4.4) and the array-to-pointer conversion
(4.2) are applied. [Note: In particular, neither the null pointer
conversion (4.10) nor the derived-to-base conversion (4.10) are applied.
Although 0 is a valid template-argument for a non-type
template-parameter of integral type, it is not a valid template-argument
for a non-type template-parameter of pointer type. ]
....
- For a non-type template-parameter of type pointer to function,
only the function-to-pointer conversion (4.3) is applied...
The important part is that null pointer conversion is not performed.
That's why you get the error. Make it
template <typename T, int (*F)(int) = static_cast<int(*)(int)>(0) >
class XXX
{
};
See also DR#354:
http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#354 . It
explains, by example, why a cast is required.
--
With best wishes,
Igor Tandetnik
With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925