c++ - 如何在模板类中声明 friend 用户定义的文字运算符?

标签 c++ c++11 g++ language-lawyer user-defined-literals

目前还不清楚为什么下面的代码不能用 GCC g++ 4.7 编译,说明如下:

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

如果类 C 被声明为非模板,那么它可以正常编译。

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

这段代码有什么问题?还是编译器错误?

最佳答案

Or is it a compiler bug?

此代码是正确的,因为标准明确允许运算符函数的签名 - 请参阅 §13.5.8/3。 所以这是一个 GCC 错误。

关于c++ - 如何在模板类中声明 friend 用户定义的文字运算符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24465229/

相关文章:

c++ - 值在常量表达式替代模式中不可用

c++ - 使用枚举类定义标志

C++11 lambdas 可以访问我的私有(private)成员。为什么?

c++ - 由于在 Bjarne Stroustrup "Programming and Practices using c++"中找不到符号导致的链接错误

c++ - 我可以告诉编译器考虑关闭关于返回值的控制路径吗?

c++ - Robai Cyton Joint Velocity 使用 Actions API

c++ - 如何在输出控制台中显示更多小数?

c++ - 使用文件内容作为硬编码字符串

c++ - 使用 Mersenne-Twister 生成多个随机值

c++ - 在 C++ 中计算 10^9 中的数字