constant-expression

Implementing the Linux Kernel's __is_constexpr (ICE_P) macro in pure C++

爷,独闯天下 提交于 2019-12-23 16:42:04
问题 After reading about the standard C11 version of Martin Uecker's ICE_P predicate, I tried to implement it in pure C++. The C11 version, making use of _Generic selection is as follows: #define ICE_P(x) _Generic((1? (void *) ((x)*0) : (int *) 0), int*: 1, void*: 0) The obvious approach for C++ is to replace _Generic by a template and decltype , such as: template<typename T> struct is_ice_helper; template<> struct is_ice_helper<void*> { enum { value = false }; }; template<> struct is_ice_helper

Will the Java compiler precalculate sums of literals?

你。 提交于 2019-12-23 16:18:26
问题 int i = 10 + 20; Is it true that the compiler will process this code, adding 10 + 20 , and the byte code is the same as for this line of code? int i = 30; Where can I read about it? 回答1: Yes, and you can even verify it for yourself. Take a small Java file, for example: public class Main { public Main() { int i = 10 + 20; } } Compile it with javac Main.java , and then run javap -c Main to disassemble it: Compiled from "Main.java" public class Main extends java.lang.Object{ public Main(); Code:

Can arrays be indexed at compile time?

∥☆過路亽.° 提交于 2019-12-23 09:49:41
问题 In this comment to another question, the user hvd stated the following: ... although string literals can be passed to constexpr functions, and array indexing is allowed on string literals in constant expressions, an indexing operation on a constexpr function parameter doesn't qualify as a constant expression. I didn't fully understand what was meant. Does it mean that the hash_value variable in the following code #include <cstddef> // Compute the hash of a string literal adding the values of

Can the C# compiler or JIT optimize away a method call in a lambda expression?

别等时光非礼了梦想. 提交于 2019-12-23 06:57:30
问题 I'm starting this question after a discussion which started (in comments) on another StackOverflow question, and I'm intrigued to know the answer. Considering the following expression: var objects = RequestObjects.Where(r => r.RequestDate > ListOfDates.Max()); Will there be any (performance) advantage of moving the evaluation of ListOfDates.Max() out of the Where clause in this case, or will 1. the compiler or 2. JIT optimize this away? I believe C# will only do constant folding at compile

`const int a = 1;` is `a` a constant expression, if `a` has automatic storage duration

自古美人都是妖i 提交于 2019-12-22 11:29:54
问题 N4527 5.20[expr.const]p2 A conditional-expression e is a core constant expression unless the evaluation of e, following the rules of the abstract machine (1.9), would evaluate one of the following expressions: (2.7) — an lvalue-to-rvalue conversion (4.1) unless it is applied to (2.7.1) — a non-volatile glvalue of integral or enumeration type that refers to a complete non-volatile const object with a preceding initialization, initialized with a constant expression, or (2.7.2) — a non-volatile

Details of what constitutes a constant expression in C?

徘徊边缘 提交于 2019-12-21 09:07:31
问题 C defines at least 3 levels of "constant expression": constant expression (unqualified) arithmetic constant expression integer constant expression 6.6 paragraph 3 reads: Constant expressions shall not contain assignment, increment, decrement, function-call, or comma operators, except when they are contained within a subexpression that is not evaluated. So does this mean 1,2 is not a constant expression? Paragraph 8 reads: An arithmetic constant expression shall have arithmetic type and shall

Dividing by zero in a constant expression

拜拜、爱过 提交于 2019-12-20 09:46:56
问题 My toy compiler crashes if I divide by zero in a constant expression: int x = 1 / 0; Is this behaviour allowed by the C and/or C++ standards? 回答1: The mere presence of 1 / 0 does not permit the compiler to crash. At most, it is permitted to assume that the expression will never be evaluated, and thus, that execution will never reach the given line. If the expression is guaranteed to be evaluated, the standard imposes no requirements on the program or compiler. Then the compiler can crash. 1 /

Is a glvalue integral constant expression a constant expression?

我怕爱的太早我们不能终老 提交于 2019-12-20 02:38:29
问题 N4527 5.20 [expr.const]p3 An integral constant expression is an expression of integral or unscoped enumeration type, implicitly converted to a prvalue, where the converted expression is a core constant expression. 5.20 [expr.const]p5 A constant expression is either a glvalue core constant expression whose value refers to an entity that is a permitted result of a constant expression (as defined below), or a prvalue core constant expression whose value is an object where, for that object and

Is the comma operator allowed in a constant-expression in C++11?

耗尽温柔 提交于 2019-12-18 12:47:49
问题 In the process of answering this question on SO for C++11, I realized that in C++03 (as well as in C) the use of the comma operator is explicitly forbidden in a constant-expression . Paragraph 5.19/1 of the C++03 Standard on constant expressions says: [...] In particular, except in sizeof expressions, functions, class objects, pointers, or references shall not be used, and assignment, increment, decrement, function-call, or comma operators shall not be used . In C++11, however, that last part

error: switch quantity not an integer

本小妞迷上赌 提交于 2019-12-18 07:37:32
问题 I have researched my issue all over StackOverflow and multi-google links, and I am still confused. I figured the best thing for me is ask... Im creating a simple command line calculator. Here is my code so far: const std::string Calculator::SIN("sin"); const std::string Calculator::COS("cos"); const std::string Calculator::TAN("tan"); const std::string Calculator::LOG( "log" ); const std::string Calculator::LOG10( "log10" ); void Calculator::set_command( std::string cmd ) { for(unsigned i = 0