在这种情况下,"typename..."意味着什么?

What does "typename..." mean in this context?

本文关键字:什么 意味着 typename 这种情况下      更新时间:2023-10-16

我在 cppreference.com 中找到了一些我不明白的代码。 这是链接:类型别名。它说的是我不明白的dependent template-id。 代码如下:

//When the result of specializing an alias template is a dependent template-id, 
//subsequent substitutions apply to that template-id:
template<typename...>
using void_t = void;
template<typename T>
void_t<typename T::foo> f();
f<int>();     // error, int does not have a nested type foo

当我将鼠标悬停在VS 2019上时,它说void_t<<未命名>...>

谁能向我解释这个未命名的字体名有什么用?

这是一个没有名称的模板包,因为它没有被使用。您可以传递任何类型,结果将是相同的。

让我试着解释一下, 表达式template<typename...> using void_t = void;,其类型推导为void,而不考虑传递的模板参数。 例

template<typename...>
using void_t = void;
template <typename T>
void_t<T> fun1() {
std::cout << "fun1 called" << std::endl;
}
int main() {
fun1<int>();   //fun1 called
fun1<float>(); //fun1 called
}

为了扩展相同的内容,template<typename T>void_t<typename T::foo> f();只接受具有嵌套类型T::footypename T。例如

template<typename...> 
using void_t = void;
template<typename T>
void_t<typename T::foo> f() {}
struct bar
{
typedef int foo;
};
int main() {
f<bar>(); //Valid expression as bar::foo is nested type of bar
f<int>(); //Error because `int::foo`is not a nested type of `int`.
}

有关详细信息,请参阅替换失败不是错误

template<typename...>
using void_t = void;

它允许您将模板参数传递给它,它只会吃掉所有参数,从而产生相同的类型。它对SFINAE有用