Why is implicit conversion not allowed in the prefecture?

Suppose we have the following code:

void ff(wchar_t*)
{

}

template<typename T>
void ffc(T&& a)
{
    ff(std::forward<T>(a));
}

Why is the call allowed ff(0)but ffc(0)not?

+4
source share
1 answer

In the case of ffc(0)T, it will be output as int, since it 0is an integer literal whose type is int, and there is no real implicit conversion from int to wchar_t * even without forwarding, so the following case won Don't work:

template<typename T>
void ffc_no_forward(T&& a)
{
    ff(a);
}

whereas in the first case it 0is a constant of a null pointer and, therefore, is an absolutely valid conversion to wchar_t *.

++ 14 (N4140) section 4.10 Pointer conversion [conv.ptr] , int , prvalue std:: nullptr_t:

(2.14.2) 0 prvalue std:: nullptr_t. ; . . [...]

++ 11 , , , T.C. point std::forward<T>() , .

+5

Source: https://habr.com/ru/post/1617679/


All Articles