In function call, why doesn't nullptr match a pointer to a template object?

后端 未结 5 1940
萌比男神i
萌比男神i 2021-02-20 11:29

Here is an example of a code that works perfectly:


#include
#include

template< class D, template< class D, class A >         


        
5条回答
  •  -上瘾入骨i
    2021-02-20 12:23

    From the C++ standard (4.10 Pointer conversions [conv.ptr])

    1 A null pointer constant is an integral constant expression (5.19) prvalue of integer type that evaluates to zero or a prvalue of type std::nullptr_t. A null pointer constant can be converted to a pointer type; the result is the null pointer value of that type and is distinguishable from every other value of object pointer or function pointer type. Such a conversion is called a null pointer conversion.

    In your first exemple your two nullptr have already been converted before template argument deduction. So there is no problem you have the same type twice.

    In the second one, there is a std::vector and a std::nullptr_t and that does not match. You have to do the conversion yourself: static_cast*>(nullptr).

提交回复
热议问题