简体   繁体   English

“type* = nullptr”是什么意思

[英]What is the meaning of “type* = nullptr”

I don't undestand.我不明白。

template<class T>
T foo2(T t, typename std::enable_if<std::is_integral<T>::value >::type* = nullptr) 
{
    return t;
}

type* = 0 ?类型* = 0 What is this.这是什么。

This is a way of achieving SFINAE : The function can only be selected if all the types can be substituted in properly.这是实现SFINAE的一种方式:只有在所有类型都可以正确替换的情况下,才能选择 function。

If std::is_integral<T>::value is false (ie, T is not an integral type), std::enable_if<...> will have no member type , thus a substitution failure happens and this function won't be called (and a different overload might).如果std::is_integral<T>::valuefalse (即T不是整数类型), std::enable_if<...>将没有成员type ,因此会发生替换失败并且此 function 不会被调用(并且可能会出现不同的重载)。

If T is an integral type, then typename std::enable_if<std::is_integral<T>::value >::type will be void , so the second parameter will be of type void* .如果T是整数类型,则typename std::enable_if<std::is_integral<T>::value >::type将为void ,因此第二个参数将为void*类型。 It is an unnamed parameter with default value nullptr , so you don't have to specify it.它是一个未命名的参数,默认值为nullptr ,因此您不必指定它。

So you would call it like this:所以你会这样称呼它:

foo2(0);  // T is `int`, which is integral, so the function can be called
foo2(0, nullptr);  // Same as above, but explicitly passing the parameter

// Can't call the function, because `double` is not integral,
// so second type is a substitution failure
// foo2(0.0);

Note that this would normally be achieved either with a default template parameter:请注意,这通常可以使用默认模板参数来实现:

// Same `void*` if integral, else substitution failure
template<class T, typename std::enable_if<std::is_integral<T>::value >::type* = nullptr>
T foo2(T t)
{
    return t;
}

// Or alternatively with an `int` if integral, else substitution failure
template<class T, typename std::enable_if<std::is_integral<T>::value, int>::type = 0>
T foo2(T t)
{
    return t;
}

Or directly in the return type:或者直接在返回类型中:

template<class T>
// Returns `T` if it's integral, else substitution failure
typename std::enable_if<std::is_integral<T>::value, T>::type foo2(T t)
{
    return t;
}

And in C++20, you can use requires (or the std::integral concept in this case)在 C++20 中,您可以使用requires (或在这种情况下使用std::integral概念)

template<class T> requires std::is_integral_v<T>
T foo2(T t)
{
    return t;
}

template<std::integral T>
T foo2(T t)
{
    return t;
}

In C/C++ you can, in a declaration of a function, omit the name of (a) parameter(s).在 C/C++ 中,您可以在 function 的声明中省略 (a) 参数的名称。 This is a preferred style sometimes when separating an interface from an implementation to avoid having confusion between the names of the parameters of, say, a function prototype & an implementation.有时,在将接口与实现分开时,这是一种首选样式,以避免混淆参数名称,例如 function 原型和实现。 I have never quite seen someone do this before.我以前从未见过有人这样做。 But what they are then doing as @IgorTandetnik pointed out, is initialize that "dummy parameter" with a default value.但是正如@IgorTandetnik 指出的那样,他们正在做的是用默认值初始化那个“虚拟参数”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM