模板类型是否有等效的“typename”?[复制]

Is there an equivalent of "typename" for template types? [duplicate]

提问人:Ad N 提问时间:7/22/2021 最后编辑:Ad N 更新时间:7/22/2021 访问量:171

问:

我们有一个表单的模板成员函数:

template <template <class> class TT>
TT<some_type> foo() const;

现在,在从依赖名称显式指定 TT 的调用上下文中:

template <class T_other>
void bar()
{
    instance.foo<T_other::template_type>();
}

Visual Studio 能够编译调用。海湾合作委员会抱怨:

error: dependent-name ‘SomeConcreteType::template_type’ is parsed as a non-type, but instantiation yields a type.

并建议说.这是行不通的,因为是模板类型,而不是普通类型。typename SomeConcreteType::template_typeSomeConcreteType::template_type

  • 这里有可以使用的关键字吗?
  • 另一种方法可以让三大编译器接受它?

编辑:现在有一个 Godbolt 最小示例 https://godbolt.org/z/hvbb6jv9W

C++ 模板 dependent-name

评论

1赞 apple apple 7/22/2021
template?...
0赞 NathanOliver 7/22/2021
instance.foo<T_other::template_type>(); -> instance.foo<typename T_other::template_type>();
0赞 Nicol Bolas 7/22/2021
我们能看到符合条件的东西的定义吗?T_other
1赞 NathanOliver 7/22/2021
我们可以使用代码获得一个最小的可重现示例吗?typename SomeConcreteType::template_type
1赞 Patrick Roberts 7/22/2021
不应该吗?(注意前面添加的关键字template <template <class> class TT>classTT)

答:

2赞 HTNW 7/22/2021 #1

使用代替 .而且,请注意,while 会在 之前 ,之后。templatetypenametypename::template

template<class T_other>
void bar() {
    instance.foo<T_other::template template_type>();
}

完整的示例

评论

0赞 Ad N 7/22/2021
您在最小示例到来之前提供了解决方案,恭喜!