Nested templated type compiles in VisualStudio but not on GCC.

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

Nested templated type compiles in VisualStudio but not on GCC

问题

以下是代码的中文翻译:

我有以下的模式:

    template<class T, int Cap>
    struct stack_collection
    {

    };

    template<int Cap>
    struct generate_stack_collection
    {
        template<class T>
        using type = stack_collection<T, Cap>
    };

    template<template<class> class Col>
    class collection_user
    {

    };

    int main()
    {
        collection_user<typename generate_stack_collection<5>::type> col;
    }

这段代码在Visual Studio/MSVC上可以编译通过,但在GCC上不行,参见:https://godbolt.org/z/W8hdM87cd 

我感觉我可能漏掉了一些 "template" / "typename" 指令,但是我似乎无法让GCC满意

我需要添加什么才能使这在GCC中编译通过

这是GCC的错误信息
```cpp
<source>: 在函数 'int main()' :
<source>:23:64: 错误:在没有参数列表的情况下无效使用模板名 'generate_stack_collection&lt;5&gt;::type'
   23 |         collection_user&lt;typename generate_stack_collection&lt;5&gt;::type&gt; col;
      |                                                                ^~~~
<source>:12:15: 注意:在此处声明了 'template&lt;class T&gt; using type = stack_collection&lt;T, 5&gt;'
   12 |         using type = stack_collection&lt;T, Cap&gt;
      |               ^~~~
<source>:23:68: 错误:模板参数 1 无效
   23 |         collection_user&lt;typename generate_stack_collection&lt;5&gt;::type&gt; col;
      |                                                                    ^
汇编生成编译器返回:1
<source>: 在函数 'int main()' :
<source>:23:64: 错误:在没有参数列表的情况下无效使用模板名 'generate_stack_collection&lt;5&gt;::type'
   23 |         collection_user&lt;typename generate_stack_collection&lt;5&gt;::type&gt; col;
      |                                                                ^~~~
<source>:12:15: 注意:在此处声明了 'template&lt;class T&gt; using type = stack_collection&lt;T, 5&gt;'
   12 |         using type = stack_collection&lt;T, Cap&gt;
      |               ^~~~
<source>:23:68: 错误:模板参数 1 无效
   23 |         collection_user&lt;typename generate_stack_collection&lt;5&gt;::type&gt; col;
      |                                                                    ^
执行生成编译器返回:1
英文:

I have the following pattern:

template&lt;class T, int Cap&gt;
struct stack_collection
{
};
template&lt;int Cap&gt;
struct generate_stack_collection
{
template&lt;class T&gt;
using type = stack_collection&lt;T, Cap&gt;;
};
template&lt;template&lt;class&gt; class Col&gt;
class collection_user
{
};
int main()
{
collection_user&lt;typename generate_stack_collection&lt;5&gt;::type&gt; col;
}

This compiles fine on visual studio/MSVC, but not on GCC, see: https://godbolt.org/z/W8hdM87cd

I have a feeling I am missing some "template" and/or "typename" directives, but I cannot seem to get GCC to be happy.

what am I missing for this to compile in GCC?

Here is what gcc says:

&lt;source&gt;: In function &#39;int main()&#39;:
&lt;source&gt;:23:64: error: invalid use of template-name &#39;generate_stack_collection&lt;5&gt;::type&#39; without an argument list
23 |         collection_user&lt;typename generate_stack_collection&lt;5&gt;::type&gt; col;
|                                                                ^~~~
&lt;source&gt;:12:15: note: &#39;template&lt;class T&gt; using type = stack_collection&lt;T, 5&gt;&#39; declared here
12 |         using type = stack_collection&lt;T, Cap&gt;;
|               ^~~~
&lt;source&gt;:23:68: error: template argument 1 is invalid
23 |         collection_user&lt;typename generate_stack_collection&lt;5&gt;::type&gt; col;
|                                                                    ^
ASM generation compiler returned: 1
&lt;source&gt;: In function &#39;int main()&#39;:
&lt;source&gt;:23:64: error: invalid use of template-name &#39;generate_stack_collection&lt;5&gt;::type&#39; without an argument list
23 |         collection_user&lt;typename generate_stack_collection&lt;5&gt;::type&gt; col;
|                                                                ^~~~
&lt;source&gt;:12:15: note: &#39;template&lt;class T&gt; using type = stack_collection&lt;T, 5&gt;&#39; declared here
12 |         using type = stack_collection&lt;T, Cap&gt;;
|               ^~~~
&lt;source&gt;:23:68: error: template argument 1 is invalid
23 |         collection_user&lt;typename generate_stack_collection&lt;5&gt;::type&gt; col;
|                                                                    ^
Execution build compiler returned: 1

答案1

得分: 3

The typename keyword is wrong here, since the type member is not actually a type, but a template. Therefore just get rid of the typename and it'll work for both compilers (msvc is technically wrong in accepting the typename variant, but eh...):

collection_user<generate_stack_collection<5>::type> col;

However, in this case you are in the situation where you are explicitly instantiating the generate_stack_collection template you have there; if you want to use a dependent template parameter, you have to use the template keyword - but in contrast to typename you add that after the :::

template<int X>
using C = collection_user<generate_stack_collection<X>::template type>;

int main()
{
    C<5> col;
    return 0;
}

This code should work in any compliant compiler (and will work in gcc/msvc on godbolt if you try it).

英文:

The typename keyword is wrong here, since the type member is not actually a type, but a template. Therefore just get rid of the typename and it'll work for both compilers (msvc is technically wrong in accepting the typename variant, but eh...):

collection_user&lt;generate_stack_collection&lt;5&gt;::type&gt; col;

However, in this case you are in the situation where you are explicitly instantiating the generate_stack_collection template you have there; if you want to use a dependent template parameter, you have to use the template keyword - but in contrast to typename you add that after the :::

template&lt;int X&gt;
using C = collection_user&lt;generate_stack_collection&lt;X&gt;::template type&gt;;

int main()
{
    C&lt;5&gt; col;
    return 0;
}

This code should work in any compliant compiler (and will work in gcc/msvc on godbolt if you try it).

huangapple
  • 本文由 发表于 2023年3月7日 21:44:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/75662767.html
匿名

发表评论

匿名网友

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

确定