This does not compile, I am obliged to set 3 within the [].
Is there a way to let the compiler computes the exact length of the array based on literals?
class Foo {
const uint8_t commands[] = {
0x90,
0x91,
0x92,
};
}
CodePudding user response:
Assuming you do not need one copy per object of that class (thus do not need it to be a real member), yes, make it static
:
class Foo
{
static inline const uint8_t commands[] = {0x90, 0x91, 0x92};
};
CodePudding user response:
Following up @Erel answer: I couldn't do an inline so I did the following:
foo.h:
class Foo {
static const uint8_t commands[];
}
foo.cpp:
const uint8_t Foo::commands[] = {
0x90,
0x91,
0x92,
};
void Foo::bar() {
Serial.println("sizeof(commands) is ok: %d", sizeof(commands));
}
CodePudding user response:
compiler can deduce and know command's size. you don't need to pass command's size.
from c 11 you can use constexpr
with static
to declare const inside a class
class Foo {
constexpr static int commands[] = {
0x90,
0x91,
0x92,
};
};
I suppose this is a special constraint for classes, as these are supposed to be shared via header files. And this can be a potential source of inconsistences between headers. Such inconsistences are binary incompatible, can't be detected at compile time, nor linking time. Such binary incompatibility can lead to incorrect, undefined behavior, and crashes, and usualy it leads to that. The cause is very difficult to diagnose. Doing it inside a function never breaks binary incompatibility, as it always gets recompiled and relinked.