“typename…”在这个上下文中是什么意思?

huangapple go评论141阅读模式
英文:

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

问题

我在cppreference.com上找到了一些我不理解的代码。
这是链接:类型别名。它讨论了“dependent template-id”,我不理解这个概念。
以下是代码:

// 当特化别名模板的结果是dependent template-id时,
// 后续的替代将应用于该template-id:

template<typename...>
using void_t = void;
template<typename T>
void_t<typename T::foo> f();
f<int>();     // 错误,int没有名为foo的嵌套类型

当我在VS 2019上悬停在它上面时,它显示
void_t<<unnamed>>...>

有人能解释一下这个未命名的typename有什么用吗?

英文:

I found some code in cppreference.com that I don't understand.
Here is the link: Type Alias. It's talking about dependent template-id which I don't understand.
Here's the code:

//When the result of specializing an alias template is a dependent template-id, 
//subsequent substitutions apply to that template-id:

template&lt;typename...&gt;
using void_t = void;
template&lt;typename T&gt;
void_t&lt;typename T::foo&gt; f();
f&lt;int&gt;();     // error, int does not have a nested type foo

When I hover over it on VS 2019 it says
void_t&lt;&lt;unnamed&gt;...&gt;

Can anyone explain to me how is this unnamed typename useful?

答案1

得分: 4

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

英文:

It's a template pack without name because it's not used. You can pass any types and the result will be the same.

答案2

得分: 1

template<typename...>
使用 void_t = void;

它允许您将模板参数传递给它,然后它将吞噬所有参数,得到相同的类型。这对于SFINAE很有用。

英文:
template&lt;typename...&gt;
using void_t = void;

It allows you to pass template arguments to it and it will just eat up all the arguments, resulting in the same type. It is useful for SFINAE

答案3

得分: 1

以下是翻译好的代码部分:

让我尝试解释一下,
表达式 template&lt;typename...&gt; using void_t = void;,其类型被推导为 void,不管传递的模板参数是什么。
示例:

template&lt;typename...&gt;
using void_t = void;

template &lt;typename T&gt;
void_t&lt;T&gt; fun1() {
    std::cout &lt;&lt; &quot;fun1 called&quot; &lt;&lt; std::endl;
}

int main() {
    fun1&lt;int&gt;();   //fun1 called
    fun1&lt;float&gt;(); //fun1 called
}

为了扩展相同的概念,template&lt;typename T&gt;void_t&lt;typename T::foo&gt; f(); 仅接受具有嵌套类型 T::footypename T。例如,

template&lt;typename...&gt; 
using void_t = void;

template&lt;typename T&gt;
void_t&lt;typename T::foo&gt; f() {}

struct bar
{
    typedef int foo;
};
int main() {
    f&lt;bar&gt;(); //作为 bar::foo 是 bar 的嵌套类型,这是有效的表达式
    f&lt;int&gt;(); //错误,因为 `int::foo` 不是 `int` 的嵌套类型。
}

有关更多信息,请参考 Substitution failure is not an error

英文:

Let me try to explain,
Expression template&lt;typename...&gt; using void_t = void; with type deduced to void irrespective of passed template parameter.
example,

template&lt;typename...&gt;
   using void_t = void;

template &lt;typename T&gt;
void_t&lt;T&gt; fun1() {
	std::cout &lt;&lt; &quot;fun1 called&quot; &lt;&lt; std::endl;
}

int main() {
	fun1&lt;int&gt;();   //fun1 called
	fun1&lt;float&gt;(); //fun1 called
}

To extend the same, template&lt;typename T&gt;void_t&lt;typename T::foo&gt; f(); only accepts typename T which has nested type T::foo. For example,

template&lt;typename...&gt; 
   using void_t = void;

template&lt;typename T&gt;
        void_t&lt;typename T::foo&gt; f() {}

struct bar
{
	typedef int foo;
};
int main() {
	f&lt;bar&gt;(); //Valid expression as bar::foo is nested type of bar
	f&lt;int&gt;(); //Error because `int::foo`is not a nested type of `int`.
}

For more information refer Substitution failure is not an error

huangapple
  • 本文由 发表于 2020年1月3日 16:41:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/59575431.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定