在这种情况下,"typename..."意味着什么?
What does "typename..." mean in this context?
我在 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::foo
的typename 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有用
相关文章:
- 这行代码在C++类中意味着什么
- 这对"With a stackless coroutine, only the top-level routine may be suspended."意味着什么
- @CPPFLAGS@在 Makefile.in 中意味着什么?
- 生成文件"relink"意味着什么?
- 从二进制流中读取时,将双精度变量的地址转换为 char* 意味着什么?
- 在这种情况下,"typename..."意味着什么?
- "in-situ without memory allocation" 字符串的愚蠢实现意味着什么?
- 使用typedef有什么用,它意味着什么
- GCC 的 -Wpsabi 选项究竟有什么作用?压制它意味着什么?
- 这在C++ "It does not own the underlying data, and so is cheap to copy or assign"中意味着什么
- 由于某种原因,我的代码中出现了 [json.exception.type_error.302]。我知道错误意味着什么,但我不知道哪里有故障
- 调用堆栈显示 SIGBUS,这意味着什么
- C++标准在[basic.scope.hiding]中"same scope"到底意味着什么?
- 评估对象的创建意味着什么?
- 当我们在 C++ 中说"initialize the object"时,它实际上意味着什么?
- C++这种结构"InterceptionKeyStroke &kstroke = * (InterceptionKeyStroke *) &stroke"意味着什么?
- 该语法在C 中意味着什么
- 链接标志对静态库意味着什么
- 由mpglib输出的"hip: Can't rewind stream by 74 bits"到底意味着什么?
- 这在 C# 中意味着什么以及如何使用它