隐式constexpr? [英] implicit constexpr?

查看:157
本文介绍了隐式constexpr?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以C ++ 11编译器(并且他们)注意到一个函数是一个 constexpr ,即使他们没有被声明为 constexpr

Can C++11 compilers (and do they) notice that a function is a constexpr and treat them as such even if they are not declared to be constexpr?

我在向维基百科使用示例直接演示使用 constexpr

I was demonstrating the use of constexpr to someone using the example straight from the Wikipedia:

int get_five() {return 5;}

int some_value[get_five() + 7]; // Create an array of 12 integers. Ill-formed C++



令我感到意外的是编译器没有问题。所以,我进一步改变了get_five()取一些int参数,乘以它们并返回结果,而仍然没有明确声明为 constexpr 。编译器也OK。看来,如果编译器可以做到这一点,没有太多的点,有必要的限制,以便显式声明一个 constexpr

To my surprise the compiler was OK with it. So, I further changed get_five( ) to take a few int parameters, multiply them and return the result while still not being explicitly declared to be constexpr. The compiler was OK with that as well. It seems that if the compiler can do this there isn't much point to having the restrictions that are required in order to explicitly declare something constexpr.

推荐答案

在正常运行的C ++ 11编译器上,您的代码将被拒绝。

On a properly-functioning C++11 compiler, your code would be rejected.

基于它的被接受,你几乎肯定使用gcc(或者那些密切模仿它的bug)。 gcc [取决于某些标志]可以接受任何度量不是常数的数组大小(例如,取决于来自用户的运行时输入),因为它们支持C ++中的C99可变长度数组的类比。

Based on its being accepted, you're almost certainly using gcc (or something that closely emulates its bugs). gcc [depending somewhat on flags] can accept array sizes that aren't constant by any measure (e.g., depend on run-time input from the user) because they support an analog of C99 variable-length arrays in C++.

这篇关于隐式constexpr?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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