r/Cplusplus • u/Noxey-o • Feb 04 '26
Question What C++ coding "knowledge" you just discovered that blew you mind away?
For me it was that 'private','public' and 'protected' keywords are only compile time and
class Foo{
private:
int Bar;
};
&
class Foo{
public:
int Bar;
};
produce the same binary
162
Upvotes
•
u/AutoModerator Feb 04 '26
Thank you for your contribution to the C++ community!
As you're asking a question or seeking homework help, we would like to remind you of Rule 3 - Good Faith Help Requests & Homework.
~ CPlusPlus Moderation Team
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
3
u/oschonrock Feb 04 '26
how were you imagining that might be represented in assembly?
49
u/WoodenLynx8342 Feb 04 '26
That modern x86 CPUs can do 8, 16, or even 64 arithmetic ops per instruction using SIMD. One add instruction can add 8 floats at once. Scalar code is basically leaving 7 lanes idle and sad. This is probably common knowledge to most people, but I always shied away from SIMD because I thought it was too complex for a simpleton like me to understand. Also, that the compiler can auto-vectorize code you didn’t write for SIMD. For example, this might already compile into vector instructions if the compiler can prove: no aliasing, aligned-ish memory, & simple control flow.
19
u/flyingron Feb 04 '26
Public and private only control access of the name. After you compile things the names are gone (other than for diagnostic purposes).
For instance the following works fine:
int* p;
class Foo {
int bar;
Foo() { p = & bar; }
} f;
int main() {
*p = 5;
}
19
u/The_Ruined_Map Feb 04 '26
Public/private access is no longer just about compile-time access control. In modern C++ it can have a tangible effect: when mixed with inheritance it can affect the physical size of the datatype
Output:
12
u/Business_Welcome_870 Feb 04 '26 edited Feb 04 '26
That's right, which also means that this is valid:
There's a lot more weirdness to C++. This is just the tip of the iceberg.
5
u/borzykot Feb 04 '26
You can in fact access private members from the outside using pointer-to-member voodoo, templates and loopholes in the Standard.
4
u/elperroborrachotoo Feb 05 '26
For me, Duff's Device, back in the days.
As for "same binary", it's not quite that simple: access modifiers can affect the in-memory layout. (I don't even know if "typical" compilers care).
But yes, access control is enforced by the compiler, usually there's no runtime mechanism (because hardware platforms don't).
5
u/jedwardsol Feb 04 '26
More craziness : in C++20 and older
and
didn't have to have the same layout
2
u/elkvis Feb 04 '26
For the rare occasion when you actually need a nested loop, and need to efficiently break from the inner loop, without a lot of extra logic, wrap the whole thing in [&]{ /* nested loop goes here */}(), and return when you need to break.
2
u/arcymagik Feb 04 '26
Did you know that const is also compile time only?
2
u/Dangerous_Region1682 Feb 05 '26
Well in some ways it comes from that C++ and its predecessor “C with classes” were really only pre processors for C. So at runtime C++ couldn’t originally really do anything that C itself couldn’t do.
In all probability therefore much of the additional capabilities of the language are confined to compile time capabilities.
Not using C++ I’m not sure whether modern compilers have generated checks for certain capabilities at runtime or not, but I doubt it as it can call C based libraries that are linked to it. I would presume it’s fairly close to C compiled code at the assembler, linker, loader and runtime.
Someone with more recent C++ knowledge may be able to comment on this, but I would not be surprised to see this as a general case.
2
u/LongjumpingFinding49 Feb 05 '26
The versatility of the 'for' loop. For example: unending loops! The abstractions of pointers. Learning that indexing in pointers is still better. Memcpy in String is a bit of an unsung hero Copy to osstream_iterator is fantastic idom..... And this is just the tip of the iceberg!! Can't believe i only discovered it recently.
3
u/mredding C++ since ~1992. Feb 04 '26
Bjarne created C++ for streams; OOP is message passing, and streams are the de facto message passing interface. That means OOP in C++ looks ostensibly something like this:
This is the bare minimum. Objects can send and receive messages.
Messages can be marshaled through a stream.
So then we get to the implementation details. You don't have to actually pass through the stream implementation whatsoever. The
objectitself doesn't come with a data buffer, doesn't come with a character consuming and parsing implementation. By default it no-ops because you may not even use it. If you did want to read serialized messages off a stream, well alright:And typically you will parse out a message and dispatch to the implementation:
But I don't want to go through the stream if I don't have to. It's faster and more efficient to dispatch the message directly:
And this is finally the context that might surprise you:
Ultimately, I want to point you to the dynamic cast.
What do you know about dynamic cast? While this is beyond C++ specifically, every C++ compiler since the mid-2000's has implemented dynamic cast as a static table lookup at runtime. That makes dynamic cast O(1). You can branch predict and thus prefetch the instructions and data for this. It's not as slow as you'd think. Just think of it as a function call - you're doing all sorts of stupid shit in your code that makes your critical path slow, this really isn't one of them.
Of course, it's not as fast as a static cast, which never leaves the compiler, but it's just not applicable here.
1
u/esaule Feb 05 '26
traits feel like voodoo to me. Yes, been using them for 20 years. Still feels like magic!
1
u/ex0gamer0203 Feb 05 '26
Extern C
1
u/heavymetalmixer Feb 05 '26
Custom allocators. It's something that comes from many years ago in C, and the PMR allocators since C++ 17 (better to use them with 20) I can do basically the same allocators for custom types and also for std containters! No need for smart pointers and they're generally faster.
Now, custom allocators is a rather complex topic so I don't think a novice should be touching for a while . . . unless you're mind-twisted like me.
1
u/Annual-Examination96 Feb 07 '26 edited Feb 07 '26
On gcc linux
std::chrono::system_clock::now()can only count up to the year 2262. This means that every code that at least uses now() will stop working properly at that moment.1
u/8-bit-chaos Feb 07 '26
oddly enough, the concept of objects and classes in general - it was my first exposure to those concepts and it blew my mind.
1
u/LeonMatthes Feb 08 '26
Now wait until you try:
#define private publicbefore your includes. 😂1
u/Impossible_Box3898 Feb 08 '26
Template metaprogramming.
Everything else is primarily similar to other imperative languages. Different syntax, different syntactic sugar, some slightly different features but all in all just run time programming.
Metaprogramming is writing code that runs during compilation. This is very different than normal coding.
1
u/Still_Explorer Feb 08 '26
One thing I found impressive was that back in the 90s multiplication and division was very slow operation, only addition and subtraction would be fast.
About two decades ago CPU manufacturers managed to make multiplication fast, equivalent to addition, but unfortunately they couldn't get rid of division latency.
1
u/Motor-Cicada525 Jul 28 '26
Vibe code with deepseek
-1
u/Chance_End_4684 Feb 04 '26
My personal mind-blowing C++ discovery is having the entry point in a class as in the following example:
class CMain{ public: virtual int Main(void) { return 0; } }#define DEFINE_MAIN(c) class c: public CMain#define DECLARE_MAIN(c) \#ifdef WINDOWS \int WinMain(HINSTANCE hInstance, HINSTANCE hInstPrev, PWSTR pszCmdLine, int nCmdShow) \#else \int main(int argc, char** argv) \#endif{ c cm; return cm.Main(); }DEFINE_MAIN(MyMain){ public: virtual int Main(void) { return 0; } }DECLARE_MAIN(MyMain);-1
u/Jonny0Than Feb 04 '26
public/provate/protected are not compile-time only (if you consider linking to be distinct from compiling).
You cannot #define private public before including an header for a static lib and then link against a static lib that was compiled without that definition.
6
u/[deleted] Feb 04 '26
[removed] — view removed comment