【问题标题】:Friend declaration of literal operator in class template类模板中文字运算符的友元声明
【发布时间】:2020-04-05 01:34:52
【问题描述】:

我正在尝试创建一个只能通过文字运算符构造的类,但它无法在 gcc 上构建。

这是一个精简的例子:

#include <cstddef>

template<typename C>
class Foo;
Foo<char> operator "" _foo(const char*, std::size_t);

template<typename C>
class Foo
{
    Foo() = default;
    friend Foo<char> operator "" _foo(const char*, std::size_t);
};

Foo<char> operator "" _foo(const char* str, std::size_t size)
{
    return Foo<char>();
}

int main()
{
    auto foo = "Foo"_foo;
    return 0;
}

此消息失败:https://godbolt.org/z/kfcNMR

<source>:11:22: error: 'Foo<char> operator""_foo(const char*, std::size_t)' has invalid argument list

它适用于 clang 和 msvc,没有模板它适用于 gcc:https://godbolt.org/z/exfm5Q

这是编译器错误还是我犯了错误?

【问题讨论】:

  • 我感觉这是一个编译器错误。 Clang 接受它。
  • 请注意,如果你做一个专门的Foo&lt;char&gt;,它就可以工作。

标签: c++ c++11 gcc g++


【解决方案1】:

这是一个错误,它是 5 年前报告的。

错误 61648 - g++ 接受非模板的文字运算符朋友 类,但不适用于 模板

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61648

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-09-18
    • 2011-05-08
    • 2023-03-11
    • 1970-01-01
    • 2021-05-18
    • 2011-06-07
    • 2010-11-20
    相关资源
    最近更新 更多