类模板中文本运算符的友元声明

Friend declaration of literal operator in class template

本文关键字:友元 声明 运算符 文本 中文      更新时间:2023-10-16

我正在尝试创建一个只能通过文字运算符构造的类,但它无法在 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

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

这是一个错误,5年前就被报道了。

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

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