mak*_*kuz 4 c++ namespaces friend clang
我想从全局命名空间创建一个类的朋友函数,这似乎工作正常,除非friend函数使用这样的自定义返回类型:
typedef int Type;
Type myFunction();
namespace foo {
class Foo
{
public:
friend Type ::myFunction();
private:
void bar() {}
};
}
Type myFunction()
{
foo::Foo a;
a.bar();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果int使用而不是Type代码编译,但是typedef编译器似乎没有将类型与命名空间分开,并给出错误:
error: expected a class or namespace
friend Type ::myFunction();
^
error: C++ requires a type specifier for all declarations
friend Type ::myFunction();
Run Code Online (Sandbox Code Playgroud)
我在OS X上使用clang ++ 500.2.79.我可以在示例中使用#define而不是typedef作为解决方法,但在我的实际问题中,自定义类型来自另一个头文件,无法更改.任何帮助,将不胜感激.
这适用于GCC,但在VS13中不起作用,或者如你所说,在clang ++中.但是,这解决了VS13中的问题:
friend Type (::myFunction());
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
207 次 |
| 最近记录: |