6

It is unclear why the code below does not compile with GCC g++ 4.7 telling the following:

$ g++ -std=c++11 -fPIC test.cpp 
test.cpp:11:45: error: ‘B operator"" _b(const char*, size_t)’ has invalid argument list

If class C is declared non-template then it compiles fine.

#include <cstddef>
struct B{};

B operator+(B, B) { return B(); }
B operator"" _b(const char *, size_t) { return B(); }

template<typename T>
class C
{
    friend B operator+(B, B);
    friend B operator"" _b(const char *, size_t);
};

int main() { return 0; }

What is wrong with this code? Or is it a compiler bug?

Columbo
  • 60,038
  • 8
  • 155
  • 203
dzidzitop
  • 385
  • 2
  • 11

1 Answers1

3

Or is it a compiler bug?

This code is correct, as the signature of the operator function is explicitly allowed by the standard - see §13.5.8/3. So it is a GCC-Bug.

Columbo
  • 60,038
  • 8
  • 155
  • 203
  • It's a bug. Thanks. g++ accepts literal operator friends for non-template classes but not for *template* classes. clang++ accepts this. Registered as [PR C++/61648] (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61648). – emsr Jun 29 '14 at 16:19