Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to declare friend user-defined literal operator within template class?

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?

like image 329
dzidzitop Avatar asked Jun 28 '14 08:06

dzidzitop


1 Answers

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.

like image 52
Columbo Avatar answered Oct 26 '22 23:10

Columbo