c – constexpr非静态成员函数与非constexpr构造函数(gcc,clang不同)

c – constexpr非静态成员函数与非constexpr构造函数(gcc,clang不同),第1张

概述对于此代码: 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.但是在5.x中出现以下错误: main.cpp:4:19: error: 对于此代码:
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.但是在5.x中出现以下错误:

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

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

我查看了C 14 [dcl.constexpr] / 3中的要求,其中说constexpr函数“它的每个参数类型都应该是文字类型”,但该部分没有明确提到成员函数,也没有说明是否为本条款的目的,隐含的*作为参数.

解决方法 这是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有一个跟踪器,但看起来并没有人解决它

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

总结

以上是内存溢出为你收集整理的c – constexpr非静态成员函数与非constexpr构造函数(gcc,clang不同)全部内容,希望文章能够帮你解决c – constexpr非静态成员函数与非constexpr构造函数(gcc,clang不同)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1238704.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存