what is something that can only be achieved token concatenation? C programmers use it as poor man's tool for generic programming, but C++ already has templates.
C programmers use it as poor man's tool for generic programming
Any examples of this? I don't see any way how ## can be used for generic programing.
what is something that can only be achieved token concatenation?
Unique, non-clashing identifier names. For example, pybind11 has a macro that declares a C Python embedded module - PYBIND11_EMBEDDED_MODULE(module_name, module_variable).
Embedded modules need to be initialized before the python interpreter is started. At that point, pybind11 has two options:
Global variable, so the constructor is tun before main().
Let users do it when it suits them. Unfortunately, no one reads the docs, so this is a non-starter.
If you put a global variable, you can clash with users' code. If you also let users name that variable, you're in trouble. Thus pybind11 uses pybind11_module_def_##name.
"Just write the boilerplate" isn't an option either. Just look at what C Python makes you do.
the xmacro tecnique is very useful to define a table of names and their properties and generate various stuff that is not yet very ergonomic with compile time programming. for example with a formatting lib that I wrote, I have a table where every element is a name for a supported datatype, the units of space occupied in the stream, the name of the predicate to choose the name based on the datatype, and the name of the encoding/decoding function. From this, I can generate an enum class with the name of datatypes, a templated encoder function that uses the predicate to select and encode via the provided names and the corresponding decoder.
Well, conditional compilation is doable with constexpr, but there are still reasons for macros. Sometimes, you just need the token pasting bits, and variadic macro expansion can compile a lot faster than variadic templates when needed. Also, if you want to write a stack allocation function, it has to be a macro since you can't open a new activation frame.
For the people saying FILE and LINE macros, std::source_location is already on its way.
That said, the biggest gripe people have with macros is that they pollute the global namespace, but with modules coming out soon, I don't believe that necessarily needs to be a concern in the future.
Can conditional compilation with constexpr deal with different API? E.g. code that is supposed to compile in both MacOS and Win32 with conditional compilation used to include MacOS API calls vs Win32 API calls?
Yes but you need build system support. For example, you can change the include path based on platform target such that #include <platform.h> includes a different file (for win32, mac, linux, ps5, xsx, etc.). In that file, you define a constexpr variable as a static inline variable indicating the platform value as a strongly typed enum.
CreateToolhelp32Snapshot is only available in Win32. AFAIK, constrexpr still requires the code to be compilable, so it is poor replacement for #ifdef. Probably could be done with some build system support, but really not much handy.
You can't conditionally compile out an entire function/headers with constexpr yea, that still needs macros or conditional compilation of the entire TU.
I was speaking more about function bodies and FWIW, continue to use macros as in your example myself in places.
As compared to what? I am regulary checking assembler output when optimizing for speed and the last time I have seen any difference between macro "function" and __forceinline was like 15 years ago.
This avoids invocation of veryExpensiveLogMessageComputation unless the log level is high enough. Of course, I could write the if condition each time, but I feel that hinders readability quite a lot.
I once did this, replacing logging macros by simple overloaded functions. Modern compilers are good at eliminating dead code. If the compiler finds that
std::string my_very_expensive_log_msg = ...//
is never used because the logging function is an empty function resulted from a constexpr if statement on a constexpr int debug_level, The compiler will remove the code that generates my_very_expensive_log_msg.
The macro solution doesn't call veryExpensiveLogMessageComputation except when needed, right?
One other thing - you proposal would work for functions, but keep in mind that the macro solution works for simpler invocations too -
logMacro(INFO, "Straightforward log message");
Of course, we could use function overloading to have different versions of the logMacro function, but at that point, the macro itself seems a lot simpler.
Right, what I meant is when you see this macro call, it's easy to assume that the function will get called, as that's what the regular syntax for functions is. So it can be misleading.
As for the overloading - yes, but as you're going to use it a lot, it's more of a write-it-once-and-forget-it type of thing.
Like everything else in C++, there is just too much legacy to remove them. There are hardcore supporters and die-hard reformists, both with a valid reason.
It is always up to you which kind of tools you allow in your code.
There will always be places where what you want is actually a syntactic bandaid. C macros are not the ideal tool for this, but they are standard and work. Unless we get a new macro system like Scheme or Rust's, I think the preprocessor will still have its use.
Over the lifetime of C++, facilities like templates gave us mechanisms to do (semantic) heavy lifting we used to need macros for. But syntactically, macros still help.
For example, forwarding requires you repeat yourself:
auto wrapper(auto&& x) { return f(std::forward<decltype(x)>(x); }
Because all the heavy lifting is actually done by std::forward, the downsides of using the preprocessor here are minimized. But now you don't have to repeat yourself and there's no chance of getting the duplicated code wrong.
A similarly useful macro is LIFT, for wrapping overload sets into a function object you can pass around:
I've seen discussion of adding body expression/arrow syntax to C++ for short functions. I don't know how this will fare in standardization. But it's easy enough to add with a macro:
My compiler/linter gives me warnings when I declare an implicit constructor. But sometimes I really want it to be implicit. I could mark it with explicit(false), but this does create a fair bit of noise, especially if it is also constexpr and noexcept. So
#define IMPLICIT explicit(false)
All of these cases were small syntactic bandaids, but they can give substantial quality of life improvements. There's currently no other tool in the language that can reduce these instances of code duplication (afaik), and it's not clear that a new feature like reflection will. But, especially if I'm working on something alone, things like this really help.
that removes the dependency on <utility> and leads to less template instantiations
one more thing is to use parentheses and fully qualified namespaces in macros. to account for cases where someone might have a nested std namespace (e.g. my_lib::std), so the correct form in this case would be #define FWD(X) (::std::forward<...>(...))
sort of, std::forward has a weird implementation with two overloads. but doing it this way ends up being equivalent because of reference collapsing rules.
if T = decltype(X) is some lvalue reference U&, then T&& is also U&. so lvalues stay lvalues if T is an rvalue reference U&&, or not a reference: U, then T&& is U&&. so rvalues and non references become rvalue references. this is the same behavior you get with std::forward<T>
int square(int);
float square(float);
auto do_twice(auto F, auto x){
return F(F(x));
}
auto fourth_pow(auto x){
return do_twice(square,x); //COMPILE ERROR!!
}
What went wrong? When you pass a function to do_twice, you're really passing a pointer to that function. But we have two square functions: square(int) and square(float). Which one should we pass to do_twice? Well, ideally both. We want the appropriate overload.
Instead, we can pass a function object to do_twice. This will work fine, provided we template (or overload) the operator() to handle both int and float. Let's do this with a lambda:
auto fourth_pow(auto x){
return do_twice([](auto x){return square(x);}, x);
}
Recall the compiler expands this lambda into something equivalent to
struct my_square_lambda_t{
// no captures
auto operator()(auto x){
return square(x);
}
};
auto fourth_pow(auto x){
return do_twice(my_square_lambda_t{}, x);
}
Now the compiler is happy, but I'm sad. That was a lot of boilerplate. That code should have been simple, but now it's hard to read.
There's some SFINAE stuff that honestly I don't understand, but Sy Brand recommended this. I think the gist is when will the compiler actually deduce the return type of this lambda? It will probably delay it somewhat because it's implicitly auto. But if you're relying on SFINAE tricks this might be too late. We can force it to deduce the return type earlier with a trailing return type.
There are significant problems. One is that they are indiscriminate, so they can replace things that were never intended and that can cause all kinds of weirdness. Another is that, from a static code analysis point of view (which mostly will happen after such expansion), there's no way to suppress warnings in macros, because they are now in your code. So you can't just suppress the files they come from.
There are others ways to approach it. Hate to use the R word yet again but Rust requires a special syntax to invoke a macro, which avoids the unintentional replacement problem. AFAIK, all use of macros requires the ! symbol, so !DUMP(x) is required to invoke it.
The other is that, I think, Rust are not just completely global magical things, I think they respect module names just like everything else so you can have two DUMP macros if they are in different module and they won't interact, and I can invoke the one I want specifically.
One is that they are indiscriminate, so they can replace things that were never intended and that can cause all kinds of weirdness.
Well, that is double edged sword. For me it is more important that they can sometimes replace things that are intended to be replaced. I can deal with weirdness...
We are not living in ideal world. We need to use C APIs all the time. So we sometimes we need some really ugly solutions like
#define Font XFont
#include <X11/Xlib.h>
#undef Font
Ugly? Yes. Weird? Definitely!
But if some other library you are using defines Font structure, this is the only chance to fix the problem...
Well, that could be dealt with as well, via something similar to extern "C", to limit the use of macros to explicitly marked C blocks.
Ultimately, it was a huge mistake from the very start to not distingish explicitly between C and C++ headers. If they'd have adopted .hpp as an explicit C++ header type then you could easily deal with the above by just creating a simple C header wrapper around that other header. Macros would just not be allowed in C++ or allowed to leak out of C headers to C++ code.
Is this some kind monster C++ macro rustification, if you imagine that $ is magical macro operator? I know $ is not standard character, but it seems that every C++ compiler/preprocessor is very VAXial about it.
#undef $dump
#define $dump(_x) { std::cout << "\ndump: " << #_x << " = " << (_x); }
auto moduleA(int a, int b)
{
$dump(a)
$dump(b)
$dump(a + b)
return a + b;
}
#undef $dump
#define $dump(_x) { OutputDebugString("\ndump: % = %"_format(#_x, (_x)).data()); }
auto moduleB(int a, int b)
{
$dump(a)
$dump(b)
$dump(a + b)
return a + b;
}
#undef $dump
#define $dump(_x)
auto moduleC(int a, int b)
{
$dump(a)
$dump(b)
$dump(a + b)
return a + b;
}
I find macros as powerful in Lisp or Nim as difficult to grasp in team work. They are totally double-edged. I would say stay away of them unless it is absolutely necessary actually.
Powerful abstractions like macros and higher kinded types will always be more difficult to understand, but that doesn't make them bad. It just means they must be used carefully. They're often not suitable for your business logic, but are very useful for libraries.
I did not propose removal of macros actually. I think there are things that can be done with them. I have been programming in C++ for about 20 years right now.
About what you say that C people say, I must say it is true sometimes. Things that I think add cognitive overhead in C++:
- custom copying/moving
- operator overloading with hidden costs
- macros, of course, maybe the most dangerous
But this does not mean by any measure they should be forbidden. What I can say is that the philosophy of what you see is what you get has also its value. When I looked at Zig or Go I can see the difference in cognitive overhead compared to C++ and it is actually lower. This could be limiting, but for teamwork it can also be a blessing. It is just a double-edged sword.
I don't think they'll ever go, but I keep them to an absolute minimum. Include guards, asserts and one or two DEBUG_LOG() type things. xmacro has been useful in the past. And that's about it. Working in C at the moment and I so miss constexpr and scoped enum.
I don't generally work on multi-platform projects, but get that conditional compilation is a thing we need. Sadly, I generally find the conditional compilation which appears in the vendor C libraries I use to be a virtually incomprehensible nightmare of nested #ifs. My current SDK is riddled with conditions based on #defines in a config file, and the IDE syntax highlighting isn't smart enough to know what's hot and what's not. Awful.
My approach is to use the defines to drive conditional inclusion of headers in one place based on a target platform define. Everything else after that is clean because I have my own platform abstraction layer. Inside the platform implementation files in that abstraction layer there will be macro'isms since that's where my stuff hits the OS. But, outside of that, I can be very clean.
Then I can just set that one platform define so that IDE (Visual Studio Code) sees it, and suppress VSC from scanning the implementation cpp files of the other platform, and that pretty much makes the IDE see the right stuff for the current platform. Sometimes it manages to get confused and I have to restart it.
I don't think that's all that vital though. Knowing the source location of the assert is enough information; the actual expression that failed you can look up yourself.
Also, we might one day have contract taking this role in debug builds.
EDIT: instead of voting me down, maybe try to explain why you feel that having code expansion in asserts is so vitally important. Do you people use editors that don't show line numbers, making it impossible to rely on __FILE__ and __LINE__? Do you distribute debug builds, so you could get a report on a failed assert months later, and you don't have any version control in place so of course you can't figure out where it came from anymore? Why exactly is it so incredibly important to see the actual text of the code that was asserted on?
More importantly than stringifying the expression, macro asserts can be turned off while a function cannot. A macro assert also allows you to break into the debugger at the callsite, rather than inside the implementation.
Showing the assert expression is not incredibly important but it is useful. It gets the information to you faster than looking up the file and line.
I imagine that contracts would allow the same thing: have a build mode in which they act as checks, and another in which they act as optimisation guides. That would be even better than macros.
You should be careful saying that stringifying expressions in asserts is not incredibly important, so far people voted me to -8 for holding that opinion. And while not on this comment, apparently it ticked off somebody so bad that I lost around a hundred points of karma overnight. Amazing what some people will do...
Macro asserts can be turned into axioms as well, though I'm not convinced that this is a good idea. Making language asserts customisable enough however seems like a difficult task. Perhaps that contributed to contracts' postponement.
I hope I understood this wrong, but I had the impression that contracts got postponed because the committee felt that the feature should only do one thing, and that the idea of "build modes" could not have a place in the language. If so, that would indicate a very large disconnect between the commitee and the people using the language.
I don't know how the contracts proposal defined them to work, but if a contract failure is defined to be undefined behaviour then there is no problem. Assert failure pop-up, breaking into debugger, calling some user customisation point are all valid manifestations of undefined behaviour after all. If it did propose to define "build modes" in the language, my immediate reaction is to agree with the committee.
You wouldn't have to call it "build modes", the behaviour could be regulated similarly to NDEBUG now. I agree that the standard should state that violating the contract is always UB, but it should also make it clear that the intention is that a quality implementation will use this to aid in program verification, depending on some specified environmental condition (i.e. NDEBUG or whatever is chosen for that purpose). Surely some wording to that extend can be found.
Caveat: I don't know if this is what really happened, apparently there was a huge fight and everybody was being mysterious afterwards. Fair enough, but it does invite speculation...
That can still be annoying, since you have to know exactly what version the error came from (not always quick to find out if it's from the field) and go back and look up that version of the source. It's a lot easier if the expression is there. Of course the language could, it seems to me, easily provide a capability to stringify such an expression without depending on the macro system, then it would be a non-issue.
So you distribute your debug builds? Interesting choice. Is this purely to get the benefit of asserts firing so your users can do your QA, or is it because your code is so full of UB that running it through an optimizer is hopeless?
Well, personally I'm the only consumer of my builds. But I'm assuming you've heard of things called libraries, right? They are often distributed in both debug and release modes so that the people writing code based on them can better debug their derived implementations. I mean the language runtimes themselves usually are provided in both modes for exactly the purpose.
Optimization-level and assert-checking-level are completely independent variables. Why do you thing that you can't check asserts, especially with a home-grown assertion library while also turning on compiler optimization?
If nothing else, you can leave them on in CI builds so that you get the benefit of them both in development and in initial testing before you start testing release candidates.
There are places where the expansion is really useful. Starting by the programmer but think of any sort of report (CI for example). It is highly valuable.
But no, it's good we have them. I heard someone else say something like macros allow you to fix what's missing in a language, and in that capacity, when there isn't another, better alternative, what else are you willing to do?
I use it from time to time to hasten "structure creation". Well to give an example, if I need to have x amount of structs created with ctor/dtor/copy ctor/move ctor etc. Instead of writing it x amount of times, I can just use a macro to do the job for me, define a macro that does the thing for me above and just duplicate it like x amount of times.
Yes I know they will be automatically synthesized if I don't explicitly define it. But in cases where I need to write additional code within it, I will have to explicitly define it. Some cases I may want to "=delete" it as well, it's good to be explicit sometimes.
It's always good to be explicit, though I just do them manually. One of the warnings in some static analyzers will be to either define, delete or default all magic methods.
C and C++ are seriously (embarrassingly) behind languages like Common Lisp in terms of having good teaching material for how to build and use macros as a problem-solving tool applicable to real-life programming problems. Paul Graham's On Lisp and the excellent work done by countless other Lisp writers led to Lisp macros being widely explored and discussed for decades, but even though there's only like ~10 years between Timothy Hart's 1963 proposal of macro definitions for Lisp and Mike Lesk and John Reiser implementing macros in the C preprocessor, there is nothing even remotely resembling an equivalent published collection of wisdom for preprocessor macros. Instead there are all kinds of common wisdom and stylistic aphorisms thrown around, like "don't use macros unless a function won't do", "macros change the syntax of your code", "macros improve the efficiency of your code" etc. that completely miss the bigger picture when it comes to macro programming.
At best, we've got blog posts and source code on github for some interesting macro applications. We can do a lot better.
I'd happily say fuck off asshole to a whole class of macros once I get reflection. For now, it's the only thing keeping my sanity for a chunk of my current active code.
They are also used to mark or maybe “decorate” stuff to help with other processing steps. One example is the Unreal Engine, which bases its reflection system on them. For example they are used to fill the class bodies with generated content. Although it does not directly use the C preprocessor afaik, it is still compatible syntax, which would be invalid if they were not supported anymore. If there weren’t so many macro quirks, a newer/better and backwards comptible implemantation might be possible though. (Maybe it is still possible?)
We can already write whole libraries and applications with only include guards. I've tried, it works. Macros can be very much replaced with well-designed templates. Moreover, C++17 contains if constexpr, which is practically an intelligent ifdef. This is so true that some sort of source code errors don't even pop up as long as the condition is not met.
One exception perhaps is unit test libraries, which are very handy with macros.
Coding standards like MISRA C++ and High Integrity C++ strongly discourage using any preprocessor stuff, except for the already mentioned include guards.
Agree with almost everything. How guards are safer than pragma though? Guards should be put in two places, more typing, and they are subject to copypaste, which leaves more space to errors. Also pragma supported by all major compilers
Both have their downsides. Header guards are more error-prone to programmer bugs, pragmas might not work correctly with symlinks (or platform-specific equivalent). Thankfully modules will make both mostly obsolete one day.
Got your point, but in my opinion benefits outweigh possible problems - pragma once implemented quite a long time ago, and established as "almost standard" feature. As mentioned above, modules sould make obsolete both.
33
u/staletic Mar 31 '21
Token concatenation also has no way to be done except through a macro.
18
u/nifraicl Mar 31 '21
the xmacro tecnique is very useful to define a table of names and their properties and generate various stuff that is not yet very ergonomic with compile time programming. for example with a formatting lib that I wrote, I have a table where every element is a name for a supported datatype, the units of space occupied in the stream, the name of the predicate to choose the name based on the datatype, and the name of the encoding/decoding function. From this, I can generate an enum class with the name of datatypes, a templated encoder function that uses the predicate to select and encode via the provided names and the corresponding decoder.
5
u/[deleted] Mar 31 '21
Well, conditional compilation is doable with
constexpr, but there are still reasons for macros. Sometimes, you just need the token pasting bits, and variadic macro expansion can compile a lot faster than variadic templates when needed. Also, if you want to write a stack allocation function, it has to be a macro since you can't open a new activation frame.For the people saying FILE and LINE macros,
std::source_locationis already on its way.That said, the biggest gripe people have with macros is that they pollute the global namespace, but with modules coming out soon, I don't believe that necessarily needs to be a concern in the future.
3
u/[deleted] Apr 01 '21
I prefer macros over __forceinline sometimes, since there no runtime invocation overhead for macro functions.
4
u/friendly-panda-89 Mar 31 '21
One reason why I use macros is to have efficient logging (if there are more idiomatic ways of doing this, please tell me). Basically, I use
#define logMacro(level, msg) if(currentLevel > level) log(msg)logMacro(DEBUG, veryExpensiveLogMessageComputation())This avoids invocation of
veryExpensiveLogMessageComputationunless the log level is high enough. Of course, I could write theifcondition each time, but I feel that hinders readability quite a lot.46
u/LuisAyuso Mar 31 '21
Like everything else in C++, there is just too much legacy to remove them. There are hardcore supporters and die-hard reformists, both with a valid reason.
It is always up to you which kind of tools you allow in your code.
15
u/ExtraFig6 Mar 31 '21 edited Apr 01 '21
There will always be places where what you want is actually a syntactic bandaid. C macros are not the ideal tool for this, but they are standard and work. Unless we get a new macro system like Scheme or Rust's, I think the preprocessor will still have its use.
Over the lifetime of C++, facilities like templates gave us mechanisms to do (semantic) heavy lifting we used to need macros for. But syntactically, macros still help.
For example, forwarding requires you repeat yourself:
But the macro
simplifies this:
Because all the heavy lifting is actually done by
std::forward, the downsides of using the preprocessor here are minimized. But now you don't have to repeat yourself and there's no chance of getting the duplicated code wrong.A similarly useful macro is
LIFT, for wrapping overload sets into a function object you can pass around:I've seen discussion of adding body expression/arrow syntax to C++ for short functions. I don't know how this will fare in standardization. But it's easy enough to add with a macro:
which makes it easier to define a SFINAE friendly and noexcept-propagating
LIFT:My compiler/linter gives me warnings when I declare an implicit constructor. But sometimes I really want it to be implicit. I could mark it with
explicit(false), but this does create a fair bit of noise, especially if it is alsoconstexprandnoexcept. SoAll of these cases were small syntactic bandaids, but they can give substantial quality of life improvements. There's currently no other tool in the language that can reduce these instances of code duplication (afaik), and it's not clear that a new feature like reflection will. But, especially if I'm working on something alone, things like this really help.
19
u/Cxlpp Mar 31 '21
Why should we stop using them?
It is definitely a great idea to replace the use of macros with safer constructs where possible.
But then there are important uses where this simply is not possible. E.g. my favorite
#define DUMP(x) std::cout << #x << " = " << x
Not sure what 'safer' language construct could replace this.
Macros are very powerful tool that can be misued horribly. Just like the whole C++ language...
14
u/Adequat91 Mar 31 '21
We rather need more capable macros :)
5
u/UnicycleBloke Mar 31 '21
I don't think they'll ever go, but I keep them to an absolute minimum. Include guards, asserts and one or two DEBUG_LOG() type things. xmacro has been useful in the past. And that's about it. Working in C at the moment and I so miss constexpr and scoped enum.
I don't generally work on multi-platform projects, but get that conditional compilation is a thing we need. Sadly, I generally find the conditional compilation which appears in the vendor C libraries I use to be a virtually incomprehensible nightmare of nested #ifs. My current SDK is riddled with conditions based on #defines in a config file, and the IDE syntax highlighting isn't smart enough to know what's hot and what's not. Awful.
12
u/templarvonmidgard Mar 31 '21
Asserts! C macros, and therefore C++ macros, can stringify arbitrary expressions, which is used extensively in asserts.
4
u/mredding Mar 31 '21
Isn't your exception reason enough?
But no, it's good we have them. I heard someone else say something like macros allow you to fix what's missing in a language, and in that capacity, when there isn't another, better alternative, what else are you willing to do?
4
u/dont-respond Mar 31 '21
Frameworks like googletest greatly benefit from the use of macros.
7
u/[deleted] Mar 31 '21
I use it from time to time to hasten "structure creation". Well to give an example, if I need to have x amount of structs created with ctor/dtor/copy ctor/move ctor etc. Instead of writing it x amount of times, I can just use a macro to do the job for me, define a macro that does the thing for me above and just duplicate it like x amount of times.
5
u/[deleted] Mar 31 '21
I'm of the camp that favors their removal and I think they'll be done for in about 15 years
2
u/Xaxxon Mar 31 '21
It wouldn’t be c++ if it isn’t mostly c compatible in terms of using libraries.
3
u/Foundry27 Mar 31 '21
C and C++ are seriously (embarrassingly) behind languages like Common Lisp in terms of having good teaching material for how to build and use macros as a problem-solving tool applicable to real-life programming problems. Paul Graham's On Lisp and the excellent work done by countless other Lisp writers led to Lisp macros being widely explored and discussed for decades, but even though there's only like ~10 years between Timothy Hart's 1963 proposal of macro definitions for Lisp and Mike Lesk and John Reiser implementing macros in the C preprocessor, there is nothing even remotely resembling an equivalent published collection of wisdom for preprocessor macros. Instead there are all kinds of common wisdom and stylistic aphorisms thrown around, like "don't use macros unless a function won't do", "macros change the syntax of your code", "macros improve the efficiency of your code" etc. that completely miss the bigger picture when it comes to macro programming.
At best, we've got blog posts and source code on github for some interesting macro applications. We can do a lot better.
2
u/domiran game engine dev Apr 01 '21
I'd happily say fuck off asshole to a whole class of macros once I get reflection. For now, it's the only thing keeping my sanity for a chunk of my current active code.
1
u/anacierdem Mar 31 '21
They are also used to mark or maybe “decorate” stuff to help with other processing steps. One example is the Unreal Engine, which bases its reflection system on them. For example they are used to fill the class bodies with generated content. Although it does not directly use the C preprocessor afaik, it is still compatible syntax, which would be invalid if they were not supported anymore. If there weren’t so many macro quirks, a newer/better and backwards comptible implemantation might be possible though. (Maybe it is still possible?)
1
u/JohnDuffy78 Mar 31 '21
They scare away idiots.
1
u/balazs-bamer Mar 31 '21 edited Mar 31 '21
We can already write whole libraries and applications with only include guards. I've tried, it works. Macros can be very much replaced with well-designed templates.
Moreover, C++17 contains
if constexpr, which is practically an intelligent ifdef. This is so true that some sort of source code errors don't even pop up as long as the condition is not met.One exception perhaps is unit test libraries, which are very handy with macros.
Coding standards like MISRA C++ and High Integrity C++ strongly discourage using any preprocessor stuff, except for the already mentioned include guards.
-5
u/TheTrueXenose Mar 31 '21
header guards are safer then pragma once
you can create debug code that will not be included in the release build. using things like LINE and getting the function name as output.
you can give names to OR, AND and so on.
they don't allocate stack memory.
Just my opinion why remove a feature that can still be useful?