constexpr非静态成员函数与非constexpr构造函数(gcc,clang不同) [英] constexpr non-static member function with non-constexpr constructor (gcc,clang differ)

查看:1246
本文介绍了constexpr非静态成员函数与非constexpr构造函数(gcc,clang不同)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于此代码:

struct S
{
    S(int m): m(m) {}
    constexpr int f() const { return m; }

    int m;
};

int main() { S s(1); }

编译时没有任何警告或错误,clang 3.6,3.7和3.8与 -std = c ++ 14 。但在g ++ 5.x中会出现以下错误:

it is compiled with no warnings or errors by clang 3.6, 3.7 and 3.8 with -std=c++14. But in g++ 5.x the following errors occur:

main.cpp:4:19: error: enclosing class of constexpr non-static member function 'int S::f() const' is not a literal type
     constexpr int f() const { return m; }
               ^
main.cpp:1:8: note: 'S' is not literal because:
 struct S
        ^
main.cpp:1:8: note:   'S' is not an aggregate, does not have a trivial default constructor, and has no constexpr constructor that is not a copy or move constructor

哪个编译器是正确的,为什么?

Which compiler is correct and why?

我看过C ++ 14中的要求[dcl.constexpr] / 3这说明对于 constexpr 函数,其每个参数类型应为字面类型,但该部分没有明确提及成员函数,并且不说明隐含 *

I looked at the requirements in C++14 [dcl.constexpr]/3 which says that for a constexpr function "each of its parameter types shall be a literal type", but that section does not explicitly mention member functions, and does not say whether the implied *this counts as a parameter for the purposes of this clause.

推荐答案

这是为C ++ 14修复的核心缺陷

It's a core defect that was fixed for C++14

http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1684

Clang已修补

https://groups.google.com/a/isocpp.org/forum/#!topic/std-discussion/6jM8M8FUs30

有一个GCC跟踪器,但它看起来不像任何人处理它

There's a tracker for GCC but it doesn't look like anyone has addressed it yet

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

这篇关于constexpr非静态成员函数与非constexpr构造函数(gcc,clang不同)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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