Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

friend function in global namespace with custom return type

I'd like to make a friend function of a class from a global namespace, which seems to work fine, unless the friend function uses a custom return type like this:

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;
}

If int is used instead of Type the code compiles, but with the typedef the compiler does not seem to separate the type from the namespace, and gives an error:

error: expected a class or namespace
        friend Type ::myFunction();
               ^
error: C++ requires a type specifier for all declarations
        friend Type ::myFunction();

I'm using clang++ 500.2.79 on OS X. I could use #define instead of typedef as a workaround in the example, but in my real-world issue the custom type is coming from another header file, which cannot be changed. Any help would be appreciated.

like image 873
makuz Avatar asked Jun 21 '14 16:06

makuz


1 Answers

This works in GCC, but doesn't work in VS13, or as you said, in clang++. However, this fixed the problem in VS13:

friend Type (::myFunction());
like image 90
yizzlez Avatar answered Nov 20 '22 03:11

yizzlez