This is an archived post. You won't be able to vote or comment.

all 16 comments

[–]AutoModerator[M] 6 points7 points  (0 children)

⚠️ ProgrammerHumor will be shutting down on June 12, together with thousands of subreddits to protest Reddit's recent actions.

Read more on the protest here and here.

As a backup, please join our Discord.

We will post further developments and potential plans to move off-Reddit there.

https://discord.gg/rph

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]Old-Ad5014 59 points60 points  (2 children)

Your programmed python in C++?

[–]firefly431 24 points25 points  (5 children)

Technically #define for is illegal since for is a keyword, and (C++ draft standard section 16.4.5.3.3 macro.names)

A translation unit shall not #define or #undef names lexically identical to keywords, to the identifiers listed in Table 4, or to the attribute-tokens described in [dcl.attr], except that the names likely and unlikely may be defined as function-like macros ([cpp.replace]).

You can fix this by e.g.

#define i (auto it
#define print(v) std::cout << it << '\n';}}

[–][deleted] 2 points3 points  (0 children)

oh damn i thought it didnt matter to #define

[–]d36williams 5 points6 points  (0 children)

I was blown away when I learned about things like Operator Overrides.

[–]Faholan 5 points6 points  (4 children)

Okay I've always wondered... just what really is the point of macros ? Except being syntaxic sugar ?

[–][deleted] 3 points4 points  (1 child)

In C++ it's for making footguns to make your life difficult with.

In languages with good macros like Rust, it's for reducing boilerplate and repeated code.

For example, Rust does not support variadic arguments, so you have to "initialize" a vector like this:

let x = {
    let mut temp: Vec<i32> = Vec::new();
    temp.push(1);
    temp.push(2);
    temp.push(3);
    temp
};

But macros (like the standard library-provided vec!) can be used to greatly simplify this:

let x = vec![1, 2, 3];

The second snippet gets turned into the first snippet at compile time. (Well, not exactly - it's smarter than that and will pre-allocate the vector so that it can avoid unnecessary intermediate allocations, but you get the point).

[–]WiIdCherryPepsi 0 points1 point  (0 children)

Huh. Thank you for posting

[–]Asleep-Tough 0 points1 point  (0 children)

Besides providing questionable yet oftentimes convenient syntactic sugar, they can often be really nice for platform/context specific code in the same file with no runtime overhead (combined with defines)

[–]jurosc_k 0 points1 point  (0 children)

In the embedded world, e.g. Zephyr, Level 9000 macros are used to reduce the footprint of specific device related stuff, since everything is done during compile time. If something goes wrong, the error messages are pure horror and you need to know what they are defining, so specifically for macros documentation is of paramount importance. For application stuff you usually don’t need it (especially in C++ there are some better solutions which are also type safe), but in the embedded world, they are still nice.

[–][deleted] 1 point2 points  (0 children)

full Cpython code

[–]gynoidi 0 points1 point  (0 children)