|
|
Home » Community » U++ community news and announcements » 2024rc1
|
Re: 2024rc1 [message #61034 is a reply to message #61033] |
Mon, 21 October 2024 12:55   |
Lance
Messages: 656 Registered: March 2007
|
Contributor |
|
|
mirek wrote on Mon, 21 October 2024 03:05
How? I have noticed that some people tend to constexpr to everything, but I fail to see a reason. If that is supposed to perform the test only in compile time, then 30 years old compiler will do that anyway. But I might be missing something perhaps?
On a second thought, you are right. A reasonably good compiler would perform the optimization anyways. I remeber a couple years ago when I dig into u++ memory allocation utilities, I saw this one
template <class T>
void memcpy_t(void *t, const T *s, size_t count)
{
if((sizeof(T) & 15) == 0)
memcpy128(t, s, count * (sizeof(T) >> 4));
else
if((sizeof(T) & 7) == 0)
memcpy64(t, s, count * (sizeof(T) >> 3));
else
if((sizeof(T) & 3) == 0)
memcpy32(t, s, count * (sizeof(T) >> 2));
else
if((sizeof(T) & 1) == 0)
memcpy16(t, s, count * (sizeof(T) >> 1));
else
memcpy8(t, s, count * sizeof(T));
}
Now I know you already counted on the compile time optimization.
Then the question becomes: what constexpr-if has to offer? Maybe speak a programmer's intention more explicitly and thus possible compiler check, like override?
|
|
|
|
|
|
Re: 2024rc1 [message #61041 is a reply to message #61039] |
Mon, 21 October 2024 18:12  |
Lance
Messages: 656 Registered: March 2007
|
Contributor |
|
|
mirek wrote on Mon, 21 October 2024 08:45Lance wrote on Mon, 21 October 2024 13:01
A compiler that encounters a non-constexpr if-statement with a constexpr conditional may issue a warning advising you to use if constexpr instead. This will ensure that compile-time evaluation will occur (even if optimizations are disabled).
Well, thinking about it, I guess actually the real benefit for me would be something else: compiler issues an error if the expression you marked constexpr is not...
Agreed. Like "override", make a programmer's intention more explicit, and do, more important than but similar, compiler check when it doesn't going as claimed by the programmer.
Reminds me of a related case, where constexpr seems to be helpful or possibly necessary.
union Flags{
int32 dummy;
struct{
byte borderLeft :3;
byte borderRight :3;
byte borderTop :3;
byte borderBottom:3;
byte halign :2;
byte valign :2; //16th bit
bool faceNotNull :1;
bool boldNotNull :1;
bool heightNotNull :1;
bool widthNotNull :1;
bool underlineNotNull:1;
bool italicNotNull :1;
bool strikeoutNotNull:1; //23rd bit
};
constexpr Flags() : dummy(0){ static_assert(sizeof(*this)==sizeof(dummy)); }
static constexpr int32 FontMask()
{
Flags f;
f.faceNotNull = true;
f.boldNotNull = true;
f.heightNotNull = true;
f.widthNotNull = true;
f.underlineNotNull = true;
f.italicNotNull = true;
f.strikeoutNotNull = true;
return f.dummy;
}
};
It's a somewhat contrived example. I am not sure ,for int32 FontMask(), if I don't it constexpr, will the code be compiled same as if I do. It's totally possible they do with todays smart and agressive as crazy compiler optimization.
|
|
|
Goto Forum:
Current Time: Wed Jun 11 08:19:35 CEST 2025
Total time taken to generate the page: 0.04527 seconds
|
|
|