A predefined macro for macro recursion in c?

Hello,

I read that traditionally recursive macros were possible in c[1] but that is no longer the case. I was wondering.. has it even been considered to add a predefined macro-specific (similar to __VA_ARGS__ and __VA_OPT__) macro like __MACRO__ to explicitly allow recursion in a macro?

E.g...

1
2
#define NOT_COMBINE(a,b) a##b
#define COMBINE(a,...) NOT_COMBINE(a, __VA_OPT__(__MACRO__(__VA_ARGS__)) 


[1] https://gcc.gnu.org/pipermail/gcc/2002-June/085287.html
Last edited on
Hasn't C++ committee been more busy inventing new ways to avoid macros?
keskiverto there are reasons for avoiding the c++ bloat, like keeping code cross language compatible for example, or just keeping code simple to understand with a glance, every time I look at c++ code I only ever see a bloated mess that simply obfuscates the purpose of the code, that is the very opposite of what c++ was invented for, to simplify code. Don't get me wrong, there are handy aspects of c++ such as classes and templates but the standard has gotten out of hand and is now in dire need of abandonment.
Hey, Linus Torvald! How ya doin'?
Pssst, keskiverto, the OP is talking about C, not C++. :)

What the C standard committee decides is up to them, C and C++ are becoming two separate languages with each new standard adopted.

Personally C++ getting away from macros/#defines is a better solution than what C has to offer.

Others who are overly obsessive C fanatics might disagree. :)
I sort of noticed that, but what can you expect from mere C++ user on C++ Forum?
@keskiverto: If only this forum had a like system, woulda given you a thumbs up for that comment XD
@George P: Go ask a new which they find easier to learn the how & why of low level programming, the C "Hello World!" or the C++ "Hello World!", I'm sure you'll find most of them call C++ confusing (the thing it set out to not be, ironic huh?) while calling C frustrating but at least understandable to some extent. Yes macros can sometimes be a problem but that's usually because the developer didn't make the macro from tested code, nor make it small enough, let's take the simple rotation macros rol & ror (may have mis-remembered their names) as an example. Now one could feasibly implement the macro in one line but for arguments sake, let's say we want the expression to be debuggable, a simple modification would look like this:

Declaration:
1
2
uintmax_t _rol( uintmax_t a, size_t b, size_t size );
#define rol( a, b ) _rol( a, b, sizeof(a) * CHAR_BIT ) 

Implementation:
1
2
3
4
5
6
7
8
9
uintmax_t _rol( uintmax_t a, size_t b, size_t bits )
{
	uintmax_t keep = ~(((uintmax_t)~0) << bits);
	uintmax_t left, right;
	b %= bits;
	left = a << b;
	right = a >> (bits - b);
	return keep & (left | right);
}


Now sure it doesn't look pretty, nor is it as flexible as the one line expression it normally is but it gets the job efficiently while also being understandable to newbies, now as for the c++ implementation, it would still use a macro because not even it's template system allows it to just skip the type, for example:

Declaration:
 
T rol<T>( T a, size_t b );

Implementation:
1
2
3
4
5
6
7
8
T rol<T>( T a, size_t b )
{
	T left, right;
	b %= bits;
	left = a << b;
	right = a >> (bits - b);
	return left | right;
}


A newbie won't necessarily know that <T> refers to a template and <T> is not exactly easy to google, if they're not being taught by someone directly then they might just think they're not smart enough to learn c++ and just give up at the first problem, since when is encouraging that mindset in newbs who can't afford lessons the "best practice" for any programming language? That's just elitism at that point, and we ALL know elitists are pricks who do NOT deserve to be in any community, which is precisely why I stop short of calling C the be all & end all of low level programming. Every language has it's pros & cons, to me the obfuscated mess that c++ has become is a con so big it is not worth using as my main, I'll happily wrap my functions and structures up with classes that hide the passing of the structure pointers but using c++ to develop the bulk of my code has become a big no no to me, it's like adding oil to water, it just does not belong.
awsdert wrote:
confusing (the thing it set out to not be, ironic huh?)

Sorry, what? C++ set out to be powerful and expressive, but I've never seen anyone claim that being "not confusing" to a novice programmer was something its designers ever expressed as a major priority. What's your basis for this claim?

C++ may be used by many places to teach programming to beginners, but I don't think it was ever intended as such, nor designed as such. It's designed to enable competent programmers to express a vast range of subtly different concepts, using a strict and precise syntax.
Last edited on
C++ is a 'professional' programming language, designed to be powerful and expressive for 'professional' programmers. Yes, it does require quite a bit of effort to get to grips with it. You have to put in the time and work to really get to grips with it. It's not really a 'script kiddie' language. For those not prepared for the work effort, there's always Scratch...
There are vastly better programming languages for teaching newcomers, yes. Scratch is great for kids - I've seen more than one under-10 start to get the programming bug using it. Python, I would think, is also a much nicer language to use for learning the rudiments of programming.

Unfortunately, the realities of education systems are that syllabuses get set in stone by people at a very hard level, and are difficult and expensive to change (for obvious reasons). So a great many novice students are stuck with trying to get to grips with the complexities and abstractions of C++, while trying to understand the rudiments of programming.
I came to C++ via c via Dec Basic via Fortran via Pascal via Dartmouth Basic (HP TSB) with a sprinkling of various Assembler languages (Dec PDP, Z80, 6502, 6800(0) ) and a topping of Cobol, Algol 68, Snobol and Lisp. So before I got anywhere near c/C++ I had a firm programming foundation.
Many education systems (especially in the UK) seem to teach Java as a first language. IMO that's worse than C++ for beginner programmers (although my knowledge of Java is minimal). At least with C++ you're not forced to get to grips with OOP right from the start...

If C++ was taught as C++ from the beginning eg use std::string, std::vector/array, std::format etc and keep the 'c' bits to a minimum (ie c-arrays, c-strings, pointers etc) then it might help those learning programming from scratch with C++.
Last edited on
Topic archived. No new replies allowed.