简体   繁体   English

功能模板失败

[英]Function template failing in clang

I have this section of code that was previously compiling fine with the Microsoft compiler. 我有这段代码,以前可以使用Microsoft编译器很好地进行编译。 I have now switched to clang and am receiving the following errors: 我现在已切换到clang,并收到以下错误:

Error 16 error : expected member name or ';' 错误16错误:预期的会员名称或';' after declaration specifiers' 声明后声明者

- --

Error 15 error : expected a qualified name after 'typename' 错误15错误:“类型名”后应为合格名称

for the line of code 用于代码行

template<typename PRIM> typename const PRIM::OutputPtrType          GetData(unsigned long index = 0) const;

Does anyone know what the problem is? 有人知道问题出在哪里吗?

Switch your typename const to const typename . 将您的typename const切换为const typename

template<typename PRIM> const typename PRIM::OutputPtrType          GetData(unsigned long index = 0) const;

The C++ Grammar rules for templates has an entry for typename which dictates: templatesC ++语法规则具有用于typename的条目,该条目指示:

typename... opt identifier opt . 类型名... opt标识符opt

typename nested-name-specifier identifier 类型名嵌套名称说明符

const isn't an identifier rather a cv-qualifier . const不是identifier而是cv-qualifier An identifier in your context is a dependent name. 您上下文中的标识符是从属名称。 which is basically PRIM::OutputPtrType , since OutputPtrType will depend on the name PRIM . 基本上是PRIM::OutputPtrType ,因为OutputPtrType将取决于名称PRIM For the second line, we can more accurately say that PRIM:: is a nested-name-specifier. 对于第二行,我们可以更准确地说是PRIM::是一个嵌套名称说明符。

EDIT 编辑

$14.6:1 When a qualified-id is intended to refer to a type that is not a member of the current instantiation ( [temp.dep.type] ) and its nested-name-specifier refers to a dependent type, it shall be prefixed by the keyword typename, forming a typename-specifier. $ 14.6:1当限定ID引用的类型不是当前实例的成员( [temp.dep.type] )且其nested-name-specifier引用从属类型时,应加前缀由关键字typename组成一个typename-specifier。 If the qualified-id in a typename-specifier does not denote a type, the program is ill-formed. 如果typename-specifier中的qualified-id不表示类型,则程序格式错误。

 typename-specifier: typename nested-name-specifier identifier typename nested-name-specifier templateopt simple-template-id 

You have to move const to the right: 您必须将const向右移动:

template<typename PRIM>
typename PRIM::OutputPtrType const GetData(unsigned long index = 0) const;
                             ^^^^^

Or to the left: 或在左侧:

template<typename PRIM>
const typename PRIM::OutputPtrType GetData(unsigned long index = 0) const;
^^^^^

In this context after the typename keyword the compiler expects a qualified name. 在这种情况下,在typename关键字之后,编译器需要一个限定名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM