Question

Here is the code:

struct foo {
  template<typename T = void>
  friend foo f() { return {}; }
};

int main() {
  auto x = f(); // clang++ can't find it, g++ can.
}

clang++ 3.4 gives:

fni2.cpp:8:12: error: use of undeclared identifier 'f'
  auto x = f(); // clang++ can't find it, g++ can.
           ^
1 error generated.

g++ 4.9.0 compiles it, but I don't think it should have. This is a related question, but there was no definitive answer. Section 15.4.2/2,4 discuss this, but neither of them say anything to suggest that friend function templates defined in-class should have different visibility from non-template friend functions defined in-class.

This is of academic interest to me only, though it did arise from a question by someone else who may have had an actual use case.

It looks like a g++ bug to me.

Was it helpful?

Solution

Yes, this is an error. I'm surprised it's finding the function. Apparently GCC fails entirely to hide function templates.

This C++03 example also compiles, so it could be a regression:

struct foo {
  template<typename T >
  friend void f( T ) { }

};

int main() {
  f( 3 ); // clang++ can't find it, g++ can.
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top