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

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

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

问题

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

  1. // 当特化别名模板的结果是dependent template-id时,
  2. // 后续的替代将应用于该template-id:
  3. template<typename...>
  4. using void_t = void;
  5. template<typename T>
  6. void_t<typename T::foo> f();
  7. 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:

  1. //When the result of specializing an alias template is a dependent template-id,
  2. //subsequent substitutions apply to that template-id:
  3. template&lt;typename...&gt;
  4. using void_t = void;
  5. template&lt;typename T&gt;
  6. void_t&lt;typename T::foo&gt; f();
  7. 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很有用。

英文:
  1. template&lt;typename...&gt;
  2. 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,不管传递的模板参数是什么。
示例:

  1. template&lt;typename...&gt;
  2. using void_t = void;
  3. template &lt;typename T&gt;
  4. void_t&lt;T&gt; fun1() {
  5. std::cout &lt;&lt; &quot;fun1 called&quot; &lt;&lt; std::endl;
  6. }
  7. int main() {
  8. fun1&lt;int&gt;(); //fun1 called
  9. fun1&lt;float&gt;(); //fun1 called
  10. }

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

  1. template&lt;typename...&gt;
  2. using void_t = void;
  3. template&lt;typename T&gt;
  4. void_t&lt;typename T::foo&gt; f() {}
  5. struct bar
  6. {
  7. typedef int foo;
  8. };
  9. int main() {
  10. f&lt;bar&gt;(); //作为 bar::foo 是 bar 的嵌套类型,这是有效的表达式
  11. f&lt;int&gt;(); //错误,因为 `int::foo` 不是 `int` 的嵌套类型。
  12. }

有关更多信息,请参考 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,

  1. template&lt;typename...&gt;
  2. using void_t = void;
  3. template &lt;typename T&gt;
  4. void_t&lt;T&gt; fun1() {
  5. std::cout &lt;&lt; &quot;fun1 called&quot; &lt;&lt; std::endl;
  6. }
  7. int main() {
  8. fun1&lt;int&gt;(); //fun1 called
  9. fun1&lt;float&gt;(); //fun1 called
  10. }

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,

  1. template&lt;typename...&gt;
  2. using void_t = void;
  3. template&lt;typename T&gt;
  4. void_t&lt;typename T::foo&gt; f() {}
  5. struct bar
  6. {
  7. typedef int foo;
  8. };
  9. int main() {
  10. f&lt;bar&gt;(); //Valid expression as bar::foo is nested type of bar
  11. f&lt;int&gt;(); //Error because `int::foo`is not a nested type of `int`.
  12. }

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:

确定