compile-time

Is is_constexpr possible in C++11?

梦想与她 提交于 2019-11-26 02:37:20
问题 Is it possible to produce a compile-time boolean value based on whether or not a C++11 expression is a constant expression (i.e. constexpr ) in C++11? A few questions on SO relate to this, but I don\'t see a straight answer anywhere. 回答1: As of 2017, is_constexpr is not possible in C++11. That sounds like an odd thing to say, so let me explain a bit of the history. First, we added this feature to resolve a defect: http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1129 Johannes

When does a constexpr function get evaluated at compile time?

心已入冬 提交于 2019-11-26 01:29:00
Since it is possible that a function declared as constexpr can be called during run-time, under which criteria does the compiler decide whether to compute it at compile-time or during runtime? template<typename base_t, typename expo_t> constexpr base_t POW(base_t base, expo_t expo) { return (expo != 0 )? base * POW(base, expo -1) : 1; } int main(int argc, char** argv) { int i = 0; std::cin >> i; std::cout << POW(i, 2) << std::endl; return 0; } In this case, i is unknown at compile-time, which is probably the reason why the compiler treats POW() as a regular function which is called at runtime.

When does a constexpr function get evaluated at compile time?

橙三吉。 提交于 2019-11-26 00:54:59
问题 Since it is possible that a function declared as constexpr can be called during run-time, under which criteria does the compiler decide whether to compute it at compile-time or during runtime? template<typename base_t, typename expo_t> constexpr base_t POW(base_t base, expo_t expo) { return (expo != 0 )? base * POW(base, expo -1) : 1; } int main(int argc, char** argv) { int i = 0; std::cin >> i; std::cout << POW(i, 2) << std::endl; return 0; } In this case, i is unknown at compile-time, which

Static assert in C

天大地大妈咪最大 提交于 2019-11-26 00:28:19
问题 What\'s the best way to achieve compile time static asserts in C (not C++), with particular emphasis on GCC? 回答1: C11 standard adds the _Static_assert keyword. This is implemented since gcc-4.6: _Static_assert (0, "assert1"); /* { dg-error "static assertion failed: \"assert1\"" } */ The first slot needs to be an integral constant expression. The second slot is a constant string literal which can be long ( _Static_assert(0, L"assertion of doom!") ). I should note that this is also implemented

Runtime vs Compile time

匆匆过客 提交于 2019-11-25 23:17:31
问题 Can anyone please give me a good understanding of whats the difference between run-time and compile-time? 回答1: The difference between compile time and run time is an example of what pointy-headed theorists call the phase distinction . It is one of the hardest concepts to learn, especially for people without much background in programming languages. To approach this problem, I find it helpful to ask What invariants does the program satisfy? What can go wrong in this phase? If the phase