【问题标题】:How to declare friend user-defined literal operator within template class?如何在模板类中声明朋友用户定义的文字运算符?
【发布时间】:2014-06-28 08:55:01
【问题描述】:

不清楚为什么下面的代码不能用 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; }

这段代码有什么问题?还是编译器的bug?

【问题讨论】:

  • Clang accepts it,我会说 GCC 中的编译器错误。
  • 行为在g++4.10 20140613中还是一样的
  • 刚刚为它添加了一个错误。谢谢大家。

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


【解决方案1】:

还是编译器的错误?

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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2020-04-05
  • 1970-01-01
  • 2017-02-09
  • 1970-01-01
  • 1970-01-01
  • 2020-11-01
  • 1970-01-01
  • 2020-01-28
相关资源
最近更新 更多