Suppose I have a static function which takes an enum and returns a cstring ptr for debugging.
The function can be constexpr but no guarantee is made that it can always be evaluated at compile time. Say it is running on a microcontroller and this is signalling events from a Bluetooth stack; e.g. device connected, disconnected, data in, etc, for context. So the parameter is not necessarily known at compile time.
Is there any value, meaning or difference in having the cstrings also defined as constexpr vs not?
A condensed example of what I mean:
#include <cstdio>
enum myEnum
{
EVENT1,
EVENT2,
EVENT3
};
static constexpr const char* foo(const myEnum i)
{
// Does having these as constexpr change anything?
/*constexpr*/ const char* text1 = "Text1";
/*constexpr*/ const char* text2 = "Text2";
/*constexpr*/ const char* text3 = "Text3";
/*constexpr*/ const char* textUndef = "TextUndef";
switch (i)
{
case EVENT1:
return text1;
case EVENT2:
return text2;
case EVENT3:
return text3;
default:
return textUndef;
}
}
int main()
{
const char* x = foo(EVENT1);
const char* y = foo(/*some value only known at runtime*/);
printf(x);
printf(y);
return 1;
}
I am compiling with gcc for cpp17 for an embedded microcontroller. Typically with either -Og or -Os.
Having the variables const char* declared constexpr in foo() doesn't add any value because you're not exploiting their constexpr-ness inside the function.
In main, you could declare x as constexpr in order to be sure that x's value is assigned at compile time. This could be useful because calling a constexpr function (even when passing literal values) doesn't guarantee that it will be evaluated at compile time. There's a catch though: you will not be able to modify the pointer anymore (x will become a const char *const, that is a const pointer to const char).
y is always evaluated at run time, so you cannot declare it constexpr.
My suggestion is to try your snippets in godbolt.org so that you can see the results with different optimizations options
Related
I have an API that needs to be as simple as possible. At the same time, strings to certain functions are "well known", and so are always string literals, and can therefore be turned into CRC32 values at compile time, like so:
function(crc32("text"), ...);
The checksumming happens at compile-time using constexpr.
I want to simplify this API so that the people I work with don't have to know this pointless details. I want to make the compile-time checksumming happen inside the function, like so:
function("text", ...);
Doing the checksumming inside the inline function does not work because the function argument is no longer constexpr. The following code will fail to compile:
inline void function (const char* text, ...) {
constexpr uint32_t hash = crc32(text); // does not work
}
This is for an emulated environment so its crucial that the checksumming happens at compile-time. There are several functions that work like this, and they will all benefit from a simplification of the API.
What options do I have to hide the fact that you have to remember to use the call to crc32?
Using rustyx idea, I made this:
struct AutoCRC {
constexpr AutoCRC(const char* str) : hash{crc32(str)} {}
constexpr AutoCRC(const uint32_t h) : hash{h} {}
const uint32_t hash;
};
But it didn't work. Compared to using a constexpr CRC32 hash directly this almost doubled the binary size: 1488 -> 2696.
I have some limited C++20 access with GCC 9.2.
Note that even in case of function(crc32("text"), ...) the context of the crc32("text") call isn't constexpr and so it isn't guaranteed to happen at compile time. You need consteval for such a guarantee.
Similar to the macro solution, you could wrap the string literal in a helper "checksummer" class that stores the string and its checksum, and accept that as an argument. This way the checksumming happens before calling the function and a good compiler will do it at compile time for strings that are known at compile time.
For example:
#include <string_view>
constexpr uint32_t checksum(std::string_view sv) noexcept {
uint32_t sum = 0;
for (unsigned char c : sv)
sum += c;
return sum;
}
struct StrWithSum {
const char* str;
const uint32_t sum;
constexpr StrWithSum(const char* p) : str(p), sum(checksum(p)) {}
};
uint32_t do_something(StrWithSum str) {
return str.sum + 2;
}
int main() {
return do_something("ABC");
static_assert(StrWithSum("ABC").sum == 198, "sum");
}
We can see that the do_something function compiles down to the + 2 operation in both GCC and clang:
_Z12do_something10StrWithSum: # do_something
lea eax, [rsi + 2]
ret
And the entire program to
main: # #main
mov eax, 200
ret
(not the case in VC++, unfortunately).
This question already has an answer here:
Is there a non-indirection, non-hack way to guarantee that a constexpr function only be callable at compile time?
(1 answer)
Closed 2 years ago.
Can you have constexpr rvalues, e.g. when initializing variables using the result of several constexpr functions?
i.e. can I guarantee that an rvalue is computed at compile time regardless of compiler settings?
constexpr int getvalue1()
{
return 42;
}
constexpr int getvalue2()
{
return 24;
}
int main()
{
// I want to initialize val with a value known at compile time
constexpr int ceval = getvalue1() + getvalue2();
int val = ceval;
// why can't I just do:
//
// int val = constexpr getvalue1() + constexpr getvalue2();
}
https://godbolt.org/z/KcK23k
Just use:
int val = getvalue1() + getvalue2();
The optimizer will take care of it. If you disable the optimizer, then yes, the compiler will issue calls to these functions, otherwise you wouldn't be able to set breakpoints and step into them.
Even if you use C++20's consteval specifier which requires the functions to produce a constant expression, the compiler will still issue calls if you disable the optimizer:
consteval int getvalue1()
{ return 42; }
consteval int getvalue2()
{ return 24; }
// ...
int val = getvalue1() + getvalue2();
So long story short: just use the optimizer. If you force the issue the way you did through a intermediate constexpr variable, all you're doing is making debugging more difficult when you end up having to actually debug constexpr or consteval functions.
With C++11 functionality, you can’t guarantee this, although most compilers will do this for such a simple case if you don’t turn optimisations off.
C++20 adds the constinit keyword for just this purpose, but this only works for static or thread-local variables.
#include <array>
int value1(int param) {
return param * 2;
}
constexpr int value2(int param) {
return param * 2;
}
int main() {
const int i = 10;
std::array<int, value1(i)> starr1 = {}; // 1
std::array<int, value2(i)> starr2 = {}; // 2
return 0;
}
2 is okay, but 1 gives a compile error because std::array has to make static size array. value2() returns compile-time constant value because of constexpr keyword.
So, how does the compiler infer that value2(i) is compile-time constant? Does it call the function value2() while compiling?
const int value1(int param) {
return param * 2;
}
int main() {
const int i = 10;
std::array<int, value1(i)> starr1 = {}; // 3
return 0;
}
>>> error: call to non-constexpr function ‘const int value1(int)’
Also, 3 still tgives a compile error. Is value1(i) not compile-time constant even though const keyword is applied to the function value1()?
So, how compiler infer value2(i) is compile-time constant?
It doesn't infer that. You state that explicitly when you annotate it with constexpr. It might infer that for functions not marked with constexpr, though. This still won't allow you to use their results in compile-time expressions, and is only used as an optimization strategy.
Does it call the function value2() while compiling?
In a sense, yes. It's probably closer to interpreting it directly, since I don't think any compiler actually compiles that function for the purposes of executing it during the build. What matters is that it's able to establish its result before the entire program is built and ran, and that it can use that result to e.g. determine the size of your array when generating the code.
Is value1(i) not compile constant even though const keyword is applied to the function value1()?
It's not. const only applies to the return type (and in this case, it's effectively useless), not the evaluation possibility in compile-time.
I get an error on line 6 (initialize my_foo to foo_init) of the following program and I'm not sure I understand why.
typedef struct foo_t {
int a, b, c;
} foo_t;
const foo_t foo_init = { 1, 2, 3 };
foo_t my_foo = foo_init;
int main()
{
return 0;
}
Keep in mind this is a simplified version of a larger, multi-file project I'm working on. The goal was to have a single constant in the object file, that multiple files could use to initialize a state structure. Since it's an embedded target with limited resources and the struct isn't that small, I don't want multiple copies of the source. I'd prefer not to use:
#define foo_init { 1, 2, 3 }
I'm also trying to write portable code, so I need a solution that's valid C89 or C99.
Does this have to do with the ORGs in an object file? That initialized variables go into one ORG and are initialized by copying the contents of a second ORG?
Maybe I'll just need to change my tactic, and have an initializing function do all of the copies at startup. Unless there are other ideas out there?
In C language, objects with static storage duration have to be initialized with constant expressions, or with aggregate initializers containing constant expressions.
A "large" object is never a constant expression in C, even if the object is declared as const.
Moreover, in C language, the term "constant" refers to literal constants (like 1, 'a', 0xFF and so on), enum members, and results of such operators as sizeof. Const-qualified objects (of any type) are not constants in C language terminology. They cannot be used in initializers of objects with static storage duration, regardless of their type.
For example, this is NOT a constant
const int N = 5; /* `N` is not a constant in C */
The above N would be a constant in C++, but it is not a constant in C. So, if you try doing
static int j = N; /* ERROR */
you will get the same error: an attempt to initialize a static object with a non-constant.
This is the reason why, in C language, we predominantly use #define to declare named constants, and also resort to #define to create named aggregate initializers.
It's a limitation of the language. In section 6.7.8/4:
All the expressions in an initializer for an object that has static storage duration shall be constant expressions or string literals.
In section 6.6, the spec defines what must considered a constant expression. No where does it state that a const variable must be considered a constant expression. It is legal for a compiler to extend this (6.6/10 - An implementation may accept other forms of constant expressions) but that would limit portability.
If you can change my_foo so it does not have static storage, you would be okay:
int main()
{
foo_t my_foo = foo_init;
return 0;
}
2021: For who reaches this post because of arm-none-eabi-gcc.exe compile error on STM32 MCUs:
Change your toolchain to gnu-tools-for-stm32.9-2020-q2-update.
From GCC V8.1+, nested constant initializer is supported and the code below will be compiled.
const int a = 1;
const int b = a +1;
typedef struct foo_t {
int a, b, c;
} foo_t;
const foo_t foo_init = { 1, 2, 3 };
foo_t my_foo = foo_init;
int main()
{
return 0;
}
arm-none-eabi-gcc.exe in gnu-tools-for-stm32.7-2018-q2-update is based on gcc v7.3.1 and the code above will not compile! But gnu-tools-for-stm32.9-2020-q2-update uses gcc v9.3.1 and will compile.
For more info see these:
Why "initializer element is not a constant" is... not working anymore?
and
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69960#c18
Just for illustration by compare and contrast
The code is from http://www.geeksforgeeks.org/g-fact-80/
/The code fails in gcc and passes in g++/
#include<stdio.h>
int initializer(void)
{
return 50;
}
int main()
{
int j;
for (j=0;j<10;j++)
{
static int i = initializer();
/*The variable i is only initialized to one*/
printf(" value of i = %d ", i);
i++;
}
return 0;
}
This is a bit old, but I ran into a similar issue. You can do this if you use a pointer:
#include <stdio.h>
typedef struct foo_t {
int a; int b; int c;
} foo_t;
static const foo_t s_FooInit = { .a=1, .b=2, .c=3 };
// or a pointer
static const foo_t *const s_pFooInit = (&(const foo_t){ .a=2, .b=4, .c=6 });
int main (int argc, char **argv) {
const foo_t *const f1 = &s_FooInit;
const foo_t *const f2 = s_pFooInit;
printf("Foo1 = %d, %d, %d\n", f1->a, f1->b, f1->c);
printf("Foo2 = %d, %d, %d\n", f2->a, f2->b, f2->c);
return 0;
}
There are basically two sorts of initialization: at compile time, and at run time.
The initialization for static-storage variable belongs to the compile-time initialization. Note that the static-storage variable includes:
global variable without the static keyword
global variable with the static keyword
local variable with the static keyword
But what is the principle behind this rule?
In my mind it's simple to explain.
Before the completion of compilation, the values of these variables would be stored into the executable file. And at that time no code can run!
gcc 7.4.0 can not compile codes as below:
#include <stdio.h>
const char * const str1 = "str1";
const char * str2 = str1;
int main() {
printf("%s - %s\n", str1, str2);
return 0;
}
constchar.c:3:21: error: initializer element is not constant
const char * str2 = str1;
In fact, a "const char *" string is not a compile-time constant, so it can't be an initializer. But a "const char * const" string is a compile-time constant, it should be able to be an initializer. I think this is a small drawback of CLang.
A function name is of course a compile-time constant.So this code works:
void func(void)
{
printf("func\n");
}
typedef void (*func_type)(void);
func_type f = func;
int main() {
f();
return 0;
}
I had this error in code that looked like this:
int A = 1;
int B = A;
The fix is to change it to this
int A = 1;
#define B A
The compiler assigns a location in memory to a variable. The second is trying a assign a second variable to the same location as the first - which makes no sense. Using the macro preprocessor solves the problem.
Is there a difference between the following definitions?
const double PI = 3.141592653589793;
constexpr double PI = 3.141592653589793;
If not, which style is preferred in C++11?
I believe there is a difference. Let's rename them so that we can talk about them more easily:
const double PI1 = 3.141592653589793;
constexpr double PI2 = 3.141592653589793;
Both PI1 and PI2 are constant, meaning you can not modify them. However only PI2 is a compile-time constant. It shall be initialized at compile time. PI1 may be initialized at compile time or run time. Furthermore, only PI2 can be used in a context that requires a compile-time constant. For example:
constexpr double PI3 = PI1; // error
but:
constexpr double PI3 = PI2; // ok
and:
static_assert(PI1 == 3.141592653589793, ""); // error
but:
static_assert(PI2 == 3.141592653589793, ""); // ok
As to which you should use? Use whichever meets your needs. Do you want to ensure that you have a compile time constant that can be used in contexts where a compile-time constant is required? Do you want to be able to initialize it with a computation done at run time? Etc.
No difference here, but it matters when you have a type that has a constructor.
struct S {
constexpr S(int);
};
const S s0(0);
constexpr S s1(1);
s0 is a constant, but it does not promise to be initialized at compile-time. s1 is marked constexpr, so it is a constant and, because S's constructor is also marked constexpr, it will be initialized at compile-time.
Mostly this matters when initialization at runtime would be time-consuming and you want to push that work off onto the compiler, where it's also time-consuming, but doesn't slow down execution time of the compiled program
constexpr indicates a value that's constant and known during compilation.
const indicates a value that's only constant; it's not compulsory to know during compilation.
int sz;
constexpr auto arraySize1 = sz; // error! sz's value unknown at compilation
std::array<int, sz> data1; // error! same problem
constexpr auto arraySize2 = 10; // fine, 10 is a compile-time constant
std::array<int, arraySize2> data2; // fine, arraySize2 is constexpr
Note that const doesn’t offer the same guarantee as constexpr, because const
objects need not be initialized with values known during compilation.
int sz;
const auto arraySize = sz; // fine, arraySize is const copy of sz
std::array<int, arraySize> data; // error! arraySize's value unknown at compilation
All constexpr objects are const, but not all const objects are constexpr.
If you want compilers to guarantee that a variable has a value that can be
used in contexts requiring compile-time constants, the tool to reach for is constexpr, not const.
A constexpr symbolic constant must be given a value that is known at compile time.
For example:
constexpr int max = 100;
void use(int n)
{
constexpr int c1 = max+7; // OK: c1 is 107
constexpr int c2 = n+7; // Error: we don’t know the value of c2
// ...
}
To handle cases where the value of a “variable” that is initialized with a value that is not known at compile time but never changes after initialization,
C++ offers a second form of constant (a const).
For Example:
constexpr int max = 100;
void use(int n)
{
constexpr int c1 = max+7; // OK: c1 is 107
const int c2 = n+7; // OK, but don’t try to change the value of c2
// ...
c2 = 7; // error: c2 is a const
}
Such “const variables” are very common for two reasons:
C++98 did not have constexpr, so people used const.
List item “Variables” that are not constant expressions (their value is not known at compile time) but do not change values after
initialization are in themselves widely useful.
Reference : "Programming: Principles and Practice Using C++" by Stroustrup
One more example to understand the difference between const and constexp.
int main()
{
int n;
cin >> n;
const int c = n; // OK: 'c' can also be initialized at run time
constexpr int e = n; // Error: 'e' must be initialized at compile time
}
Note: constexpr normally evaluated at compile-time, but they are not guaranteed to do so unless they're invoked
in a context where a constant expression is required.
constexpr int add(int a, int b)
{
return a + b;
};
int main()
{
int n = add(4, 3); // may or may not be computed at compile time
constexpr int m = add(4,3); // must be computed at compile time
}
constexpr -> Used for compile time constant. This is basically used for run time optimization.
const -> Used for run time constant.