【问题标题】:Befriend default constructor: clang produces parse error while gcc did notBefriend 默认构造函数:clang 产生解析错误,而 gcc 没有
【发布时间】:2018-04-10 21:30:49
【问题描述】:

我遇到了一个令人困惑的问题。对于以下sn-p:

class A { };
class E
{
    friend A::A() throw();
};

我使用 Clang 6 编译此代码示例并得到“错误:'A' 的非 constexpr 声明遵循 constexpr 声明”。我也试过 clang 4.0、clang 5.0 和 gcc 5.4 都没有出现这种错误。这是 Clang6 中的错误吗?

【问题讨论】:

  • 似乎Clang says constexprA::A() 签名的一部分,并且它不允许不匹配的签名。如果在好友声明中添加constexprit works
  • 你确定在clang 4.0中不会出错吗?在 Godbolt 上,它一直错误回到 3.4.1:godbolt.org/g/m6BiiW
  • 我使用了 Clang 6.0,并将该行更改为 friend constexpr A::A() noexcept;,它工作。我用clang++ -c -Weverything -Wno-c++98-compat -Wno-c++98-compat-pedantic -Wno-padded --std=c++17 Sunshine.cpp编译。
  • 我不确定编译器生成的默认构造函数是否算作声明,但 clang 可能会抱怨,因为 [dcl.constexpr] 声明:“如果函数或函数模板的任何声明具有 constexpr 说明符,那么它的所有声明都应包含 constexpr 说明符。”编译器生成的默认构造函数是constexpr,但你的朋友声明没有将它标记为constexpr
  • @NathanOliver,我使用 clang-4.0 没有任何编译选项并且没有错误。

标签: c++ clang friend-function


【解决方案1】:

来自class.ctor#7

隐式定义的默认构造函数是 constexpr

由于class A 没有用户声明的构造函数,因此没有参数的非显式构造函数被隐式声明为默认的

然而,

friend A::A() throw();

您显式声明 A::A() 与隐式声明的构造函数(即 constexpr constructor)不匹配。

具体来说:

其函数体应为 = 默认值,或复合语句 其功能体应满足要求 constexpr 函数的函数体;

【讨论】:

    猜你喜欢
    • 2023-03-20
    • 2012-07-14
    • 1970-01-01
    • 1970-01-01
    • 2016-10-16
    • 2016-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多