简体   繁体   中英

Function template failing in clang

I have this section of code that was previously compiling fine with the Microsoft compiler. I have now switched to clang and am receiving the following errors:

Error 16 error : expected member name or ';' after declaration specifiers'

-

Error 15 error : expected a qualified name after 'typename'

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 .

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:

typename... opt identifier opt .

typename nested-name-specifier identifier

const isn't an identifier rather a cv-qualifier . An identifier in your context is a dependent name. which is basically PRIM::OutputPtrType , since OutputPtrType will depend on the name PRIM . For the second line, we can more accurately say that PRIM:: is a nested-name-specifier.

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. If the qualified-id in a typename-specifier does not denote a type, the program is ill-formed.

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

You have to move const to the right:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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