gpt4 book ai didi

c++ - 它是 C++ gcc HEAD 10.0.0 20190 相对于友元函数的错误吗

转载 作者:行者123 更新时间:2023-11-27 23:38:02 25 4
gpt4 key购买 nike

<分区>

以下程序使用 clang HEAD 10.0.0 编译

#include <iostream>

template <class T>
void f( const T & );

class A
{
public:
A( int x = 0 ) : x( x ) {}

friend void ::f( const A & );
private:
int x;
};

template <class T>
void f( const T &t )
{
std::cout << "t.x = " << t.x << '\n';
}


int main()
{
A a( 10 );
f( a );
}

程序输出为

t.x = 10

但是当使用 gcc HEAD 10.0.0 20190 编译器时,它会输出错误

prog.cc:11:32: error: 'void f(const A&)' should have been declared inside '::'
11 | friend void ::f( const A & );
| ^

是编译器的问题还是我做错了什么?

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com