// struct Test; forward declaration commented void* foo(struct Test* t) // C style function parameter - This works ! { return t; }
我没有意识到这一点.我不知道它是标准的C还是扩展名,以及参数之前的struct关键字是作为前向声明还是另一个机制启动.
此外,在这样的使用之后,“下一个”功能可以使用该类型而不预先添加任何关键字:
void* oof(Test* t);
解决方法
从[basic.scope.pdecl] / 6:
[…] — for an elaborated-type-specifier of the form
class-key identifier
if the elaborated-type-specifier is used in the decl-specifier-seq or parameter-declaration-clause of a
function defined in namespace scope,the identifier is declared as a class-name in the namespace that
contains the declaration […]
例如:
namespace mine { struct T* foo(struct S *); // ^^^^^^^^^---------------- decl-specifier-seq // ^^^^^^^^^^--- parameter-declaration-clause }
6.2.1 Scopes of identifiers
4 – […] If the declarator or type specifier that
declares the identifier appears […] within the list of parameter declarations in
a function deFinition,the identifier has block scope,which terminates at the end of the
associated block. If the declarator or type specifier that declares the identifier appears
within the list of parameter declarations in a function prototype (not part of a function
deFinition),the identifier has function prototype scope,which terminates at the end of the
function declarator.
a.c:3:18: warning: ‘struct Test’ declared inside parameter list void* foo(struct Test* t) ^ a.c:3:18: warning: its scope is only this deFinition or declaration,which is probably not what you want
原文地址:https://www.jb51.cc/c/113324.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。