相对于朋友函数,这是C ++ gcc HEAD 10.0.0 20190的错误吗? [英] Is it a bug of C++ gcc HEAD 10.0.0 20190 relative to friend functions

查看:92
本文介绍了相对于朋友函数,这是C ++ gcc HEAD 10.0.0 20190的错误吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下程序使用 clang HEAD 10.0.0

  #include< iostream> 

模板< class T>
void f(const T&);

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

朋友无效: :f(const A&);
私人:
int x;
};

模板< class T>
void f(const T& t)
{
std :: cout<< t.x =<< t.x<< ‘n’;
}


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

程序输出为

  tx = 10 

但是当 gcc HEAD 10.0.0 20190 编译器被使用,然后输出错误

  prog。 cc:11:32:错误:应该在'::'
11 |中声明'void f(const A&)'。朋友无效:: f(const A&);
| ^

是编译器的错误还是我做错了?

解决方案

提起 91618




[temp.friend] / 1 读取:


一个类或类模板的朋友可以是函数模板或类模板,函数模板或类模板的特殊化或非模板函数或类。对于不是模板声明的朋友函数声明:



  • 如果朋友的名字是合格的或不合格的 template-id ,则朋友声明是函数模板的特化,否则,

  • 如果朋友的名字是 qualified-id 和匹配的非在指定的类或名称空间中找到模板函数,则Friend声明引用该函数,否则,

  • 如果朋友的名字是 qualified-id 并且在指定的类或命名空间中找到匹配的功能模板,则Friend声明引用该功能模板的推导专业化([temp.deduct.decl]),否则,

  • 名称应为声明(或重新声明)非模板函数的 unqualified-id


第三个项目符号应允许这样做:

  template< class T>无效f(T); 

结构A {
朋友void :: f(A);
};

:: f qualified-id 并找到匹配的功能模板,因此它应该可以工作。但是gcc要求我们编写 :: f<> (这是一个 template-id ),以便遵守第一个项目符号。 / p>

The following program compiles with using 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 );
}

The program output is

t.x = 10

But when the gcc HEAD 10.0.0 20190 compiler is used then it outputs the error

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

Is it a bug of the compiler or am I doing something wrong?

解决方案

Filed 91618.


[temp.friend]/1 reads:

A friend of a class or class template can be a function template or class template, a specialization of a function template or class template, or a non-template function or class. For a friend function declaration that is not a template declaration:

  • if the name of the friend is a qualified or unqualified template-id, the friend declaration refers to a specialization of a function template, otherwise,
  • if the name of the friend is a qualified-id and a matching non-template function is found in the specified class or namespace, the friend declaration refers to that function, otherwise,
  • if the name of the friend is a qualified-id and a matching function template is found in the specified class or namespace, the friend declaration refers to the deduced specialization of that function template ([temp.deduct.decl]), otherwise,
  • the name shall be an unqualified-id that declares (or redeclares) a non-template function.

That third bullet should allow this:

template <class T> void f(T);

struct A {
    friend void ::f(A);
};

::f is a qualified-id and a matching function template is found, so it should work. But gcc requires us to write ::f<>, which is a template-id, in order to adhere to the first bullet.

这篇关于相对于朋友函数,这是C ++ gcc HEAD 10.0.0 20190的错误吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆