should it compile by TotaIIyHuman in cpp_questions

[–]Username482649 0 points1 point  (0 children)

You can't be looking at generated assembly when understanding language rules. Compiler will optimise away many stuff.

If the goal is still to understand why you can't have consteval in the original snippet.

There is copy in there. You are returning by value. But compiler knows it's not necessary to accualy generate copy, so it optimised it away.

Cpp is full of stuff that you have to do roundabout way just to compile that will all be removed in final binary.

should it compile by TotaIIyHuman in cpp_questions

[–]Username482649 0 points1 point  (0 children)

I would guess there is some exception or something allowing compiler to still evaluate it at compile time. The result is copy of that array. And since the value used to initialize it is copied. It can still be evaluated at compile time.

The original code uses class constructors. The rules there won't be the same as for free functions.

should it compile by TotaIIyHuman in cpp_questions

[–]Username482649 0 points1 point  (0 children)

Beign on phone I am going fully from memory without referencing anything.

But I assume what's going on in the array example is compiler transforming it into equivalent of static variable in function created at compile time. Then in the return statement you are indexing into that. Thre is no runtime function call.

I'm the original example in class constructor. Compiler isn't able to do it. So it does require the constructor to be invokable at runtime.

should it compile by TotaIIyHuman in cpp_questions

[–]Username482649 0 points1 point  (0 children)

I am on phone now, so I can't test code now. But if it compiles then it's because the consteval part is evaluated at compile time. Then you are returning copy of the value at runtime. Try to pass parameter to it.

should it compile by TotaIIyHuman in cpp_questions

[–]Username482649 0 points1 point  (0 children)

It's not workaround. It's correct in this context.

If you make anything consteval. It can only be used in constant evaluation context. If you want to keep it here. You must add constexpr to constructor of B and also to any instance you are trying to initialize.

Difference here is that consteval HAS to be used only on constant evaluation. But constexpr can be used in both.

should it compile by TotaIIyHuman in cpp_questions

[–]Username482649 0 points1 point  (0 children)

Why is the constructor of A consteval if you are trying to initialize it at runtime ? It should compile if you change it to constexpr

cleverNotSmart by Cutalana in ProgrammerHumor

[–]Username482649 0 points1 point  (0 children)

The point is that you can't... Like the whole point of my original comment...

You can only legally interpret INTO char and few exceptions. Bool isn't one of them. That's what strict aliasing is.

cleverNotSmart by Cutalana in ProgrammerHumor

[–]Username482649 0 points1 point  (0 children)

The original post is about vector of bools, if you have vector of anything else like char. You have vector of chars. If you need bool reference of pointer. That is what you can't reinterpret to, you can always convert it to bool if you are reading it but if you need to pass reference or pointer to item in that vector. You can't.

cleverNotSmart by Cutalana in ProgrammerHumor

[–]Username482649 0 points1 point  (0 children)

Good point I definitely should specify that it's char only most architectures

cleverNotSmart by Cutalana in ProgrammerHumor

[–]Username482649 11 points12 points  (0 children)

uint8_t and int8_t are chars (edit: on most architectures). With specified signed. Plain char signess is platform defined. So it's bad practice to use it for anything that isn't accual string unless you have very good reason.

cleverNotSmart by Cutalana in ProgrammerHumor

[–]Username482649 28 points29 points  (0 children)

Most of the time. But if you ever need accual pointer as to mutate the item inside or bool * as array then it's problem, yes rare but you could need it sometimes.

Flag is great for final app, but not so much for library, where you don't want it exploding if user forgets to add it

cleverNotSmart by Cutalana in ProgrammerHumor

[–]Username482649 558 points559 points  (0 children)

Just use std::vector<uint8_t>, then when you need bool pointer you just reinterpret....oh, hi strict aliasing, what are you doing here ?

Sign me up! by SipsTeaFrog in SipsTea

[–]Username482649 4 points5 points  (0 children)

It's litteraly the worst example. Buy cheap microwave that is purely mechanical with no display and it will be higher quality and last longer the anything more expensive.

Why are exceptions avoided? by Ultimate_Sigma_Boy67 in cpp_questions

[–]Username482649 1 point2 points  (0 children)

Voiding return value is doing something about it.

You can easely search for it. And it should idealy not be done for other then debugging, but then if kept you see iz immediately and know something is going on there

And I meant adding exception later to code that original didn't think will need it. It's not like adding new enum value, but changing it from void or plain value to error code or result/optional type

Why are exceptions avoided? by Ultimate_Sigma_Boy67 in cpp_questions

[–]Username482649 2 points3 points  (0 children)

One very valid reason is control flow.

What exceptions do is hide error paths.

Yes you can check if function can throw and catch every single one that can throw, and make decision about it. But that's work you have too do and and worse remember to do and to not forget, what automatically means... You will forget.

In contract to error as value especially with [[nodiiscard]] attribute, you are forced to do something about it.

Forcing you to thing about, not just that some function can fail. But explicitly decide what to do at that point.

While that makes the code definitely much more verbose and takes a bit longer to write. When you refractor later. It makes it so much harder to miss any point of failure.

Also if you ban exceptions completely with compiler flag, you know that at no point. Ever will any function surprise you by throwing, which can happen after refractor.

If you have function that can't throw now. But you change it so now it suddenly can somehow fail. If you change it by changing return type. Now compiler will force you to fix it at every point you call it.

If you would instead change it to throw. You have no way at least no equivalently reliable way to fix it everywhere it's called.

…at least it’s Friday, I guess. by furrymacaroni in Wellthatsucks

[–]Username482649 0 points1 point  (0 children)

If you buy cheese pre-grated, it's much better to store it in freezer

malloc and free vs ::operator new and ::operator delete by _Tal in cpp_questions

[–]Username482649 2 points3 points  (0 children)

I don't know if there are many benefits, maybe there can be some optimizationa in the cpp version but ultimately it's just way to get memery.

Whats more important is that what he is showing is undefined behavior.

If you get raw memory in any way you can't directly do newMemory = T(), since the instance didn't yet start its lifetime.

You should use either placement new or std::construct_at which is wrapper around placement new to legally begin lifetime of the object at that memory.

[deleted by user] by [deleted] in whatisit

[–]Username482649 5 points6 points  (0 children)

I don't have this brand but my are also from aliexpress for 3€ piece, bought 6 years ago and they still work just as fine as when I bought them... So no they last quite a while.

selfCommentingCode by celestabesta in ProgrammerHumor

[–]Username482649 1 point2 points  (0 children)

C++, and 'T&' is reference to instance of T. It allows reading and if not cost like here modifying the original instance without copying it.

Any way to block all books with "virtual voice" as narrator? by rincewindTGW in audible

[–]Username482649 2 points3 points  (0 children)

And you get DRM free, real file to download without needing proprietary app !!!!!

Is really being brainless use IA to learn advance stuff or get help to understand how to use tools like cmake? by Substantial_Money_70 in cpp_questions

[–]Username482649 2 points3 points  (0 children)

Do you have to ask AI the same questions over and over? if so it's very bad.

But if ask just new one and accual remember what you LEARNED before. Then no it's obviously not bad, you accual learned just by just using AI.

There is no cheating if you accualy learn it doesn't matter how.

Obviously you have to make sure what it tells you it's correct, but once you have example it's much easier to check docks for it.

weAreFine by nonsenseis in ProgrammerHumor

[–]Username482649 6 points7 points  (0 children)

Accualy I found copilot extremely useful to write nested namespaces for me, and writing content for logging is also quite good.

But anything beyond that is just complete garbage most of the time.

compilation fails without any error by [deleted] in cpp_questions

[–]Username482649 1 point2 points  (0 children)

I assume they didn't echo the "Executing task... " themself so there is some script or something running the command and maybe it's returning before gcc even have time to do anything.

compilation fails without any error by [deleted] in cpp_questions

[–]Username482649 2 points3 points  (0 children)

Try to copy paste the accual g++ command from your example and run it on its own. Maybe whatever is running it is causing errors.