constant-expression

How to check if a parameter is an integral constant expression in a C preprocessor macro?

[亡魂溺海] 提交于 2019-11-30 08:41:42
问题 I'm currently cleaning up an existing C-library to publish it shamelessly. A preprocessor macro NPOT is used to calculate the next greater power of two for a given integral constant expression at compile time . The macro is normally used in direct initialisations. For all other cases (e.g. using variable parameters), there is an inline function with the same function. But if the user passes a variable, the algorithm expands to a huge piece of machine code. My question is: What may I do to

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

半腔热情 提交于 2019-11-30 08:05:20
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 mentioning the comma operator seems to be vanished. And while paragraph 5.19/2 of the C++11 Standard

Why can't a constant pointer be a constant expression?

戏子无情 提交于 2019-11-30 04:20:55
The following program compiles: template <const int * P> class Test{}; extern const int var = 42; //extern needed to force external linkage int main() { Test<&var> test; } This one, however, doesn't, which is a surprise for me: template <const int * P> class Test{}; extern const int var = 42; //extern needed to force external linkage extern const int * const ptr = &var; //extern needed to force external linkage int main() { Test<ptr> test; //FAIL! Expected constant expression. } Alternative example: int main() { const int size = 42; int ok[*&size]; //OK const int * const pSize = &size; int

error: switch quantity not an integer

拜拜、爱过 提交于 2019-11-29 13:20:37
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; i < cmd.length(); i++) { cmd[i] = tolower(cmd[i]); } command = cmd; } bool Calculator::is_legal

How to check if a parameter is an integral constant expression in a C preprocessor macro?

跟風遠走 提交于 2019-11-29 07:20:08
I'm currently cleaning up an existing C-library to publish it shamelessly. A preprocessor macro NPOT is used to calculate the next greater power of two for a given integral constant expression at compile time . The macro is normally used in direct initialisations. For all other cases (e.g. using variable parameters), there is an inline function with the same function. But if the user passes a variable, the algorithm expands to a huge piece of machine code. My question is: What may I do to prevent a user from passing anything but an integral constant expression to my macro? #define NPOT(x)

“Constant expressions” prior to C++11

自闭症网瘾萝莉.ら 提交于 2019-11-29 06:01:06
The constexpr keyword was introduced in C++11, as (I think) was the corresponding idea of "constant expressions." However, this concept was implicitly present in C++98/c++03, since array declarations require a constant expression: // valid: int a[sizeof(int)]; int b[3+7]; int c[13/4]; const int n = 3; int d[n]; // invalid: int m = 4; int e[m]; There are other "constant expressions", i.e., expressions that can be (and/or must be) evaluated at compile-time; one example is template arguments. For pre-C++11, do the following exist, either in the C++98/03 standards or elsewhere? A complete list of

Why can't a constant pointer be a constant expression?

允我心安 提交于 2019-11-29 01:39:01
问题 The following program compiles: template <const int * P> class Test{}; extern const int var = 42; //extern needed to force external linkage int main() { Test<&var> test; } This one, however, doesn't, which is a surprise for me: template <const int * P> class Test{}; extern const int var = 42; //extern needed to force external linkage extern const int * const ptr = &var; //extern needed to force external linkage int main() { Test<ptr> test; //FAIL! Expected constant expression. } Alternative

`static constexpr` function called in a constant expression is…an error?

梦想与她 提交于 2019-11-28 10:43:57
I have the following code: class MyClass { static constexpr bool foo() { return true; } void bar() noexcept(foo()) { } }; I would expect that since foo() is a static constexpr function, and since it's defined before bar is declared, this would be perfectly acceptable. However, g++ gives me the following error: error: ‘static constexpr bool MyClass::foo()’ called in a constant expression This is...less than helpful, since the ability to call a function in a constant expression is the entire point of constexpr . clang++ is a little more helpful. In addition to an error message stating that the

Example of something which is, and is not, a “Constant Expression” in C?

痴心易碎 提交于 2019-11-28 09:18:15
I'm a tad confused between what is and is not a Constant Expression in C, even after much Googleing. Could you provide an example of something which is, and which is not, a Constant Expression in C? A constant expression can be evaluated at compile time. That means it has no variables in it. For example: 5 + 7 / 3 is a constant expression. Something like: 5 + someNumber / 3 is not, assuming someNumber is a variable (ie, not itself a compile-time constant). There is another subtlety to constant expressions. There are some things that are known to the compiler, but cannot be known to the

“Constant expressions” prior to C++11

浪尽此生 提交于 2019-11-27 23:34:11
问题 The constexpr keyword was introduced in C++11, as (I think) was the corresponding idea of "constant expressions." However, this concept was implicitly present in C++98/c++03, since array declarations require a constant expression: // valid: int a[sizeof(int)]; int b[3+7]; int c[13/4]; const int n = 3; int d[n]; // invalid: int m = 4; int e[m]; There are other "constant expressions", i.e., expressions that can be (and/or must be) evaluated at compile-time; one example is template arguments.