Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is a friend function template defined in the class available for lookup? clang++ and g++ disagree

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.

like image 503
kec Avatar asked May 04 '14 02:05

kec


1 Answers

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.
}
like image 99
Potatoswatter Avatar answered Oct 21 '22 10:10

Potatoswatter