Useful #define Statements

lxXTaCoXxl -4 Tallied Votes 266 Views Share

These are just some of the more useful #define statements I've used in C++. These are some of the statements you'll use the most (in the case of template<typename T> when you use it, you use it a lot!). So I figured I'd post it for others, even though a lot of the programmers probably already have this list.

Lucaci Andrew commented: -1
#define out cout<<
#define in cin>>
#define n "\n"
#define nn "\n\n"
#define type template<typename T>
#define sp system("pause");
#define f_loop_l for (int i = 0; i < x; i++) // Replace 0 and/or x with a specific number
#define f_loop_g for (int i = 0; i > x; i++) // Replace 0 and/or x with a specific number
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Wow, that's a hideous abuse of the preprocessor.

sepp2k 378 Practically a Master Poster

None of those really seem necessary (or helpful) and some seem like incredibly bad ideas.

Your out and in macros, will make your code look very strange. If you want to print an item it will look like out foo, which really doesn't make sense syntactically. out(foo) or out << foo would make sense, but out foo doesn't look like C++ at all. And if you want to print two items, it would look like out foo << bar, which just looks totally incosistent.

Your first loop macro are pretty pointless with x not being a macro parameter. Having to insert the value you want by hand kind of defeats the purpose of having a macro (and even if x were a parameter, that's still not really something you need a macro for).

Your second loop macro is just completely pointless. If x is zero or positive, the loop won't run at all. And if x is negative, the loop will run forever (or more correctly it will invoke undefined behavior (signed integer overflow) that might result in an infinite loop - or not). Either way it's not useful.

mike_2000_17 2,669 21st Century Viking Team Colleague Featured Poster

So I figured I'd post it for others, even though a lot of the programmers probably already have this list.

Well, I guess I didn't have that list... I think that I will now add it to my list of horrible things that YOU SHOULD NOT DO with MACROs.

micwide 0 Newbie Poster
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

In this article we try to abuse the preprocessor so much that C++ no longer looks like C++.

#include <iostream>

#define also ,
#define stop ;
#define begin {
#define end }
#define args(x) (x)
#define scalar int
#define letter char
#define ref(x) x*
#define seq(x, n) x[n]
#define start main
#define print std::cout <<
#define nl << std::endl
#define from(x) = x;
#define to(x) i < x; i++)
#define repeat_with(x) for (int x
#define index(a, i) a[i]

scalar start args(scalar count also ref(letter) seq(args,))
begin
    repeat_with(i) from(0) to(count)
    begin
        print index(args, i) nl stop
    end
end
NathanOliver commented: My boss would kill me if he saw this +9
major_tom3 commented: The finest bit of satire I have seen on a C++ thread. Made my day! +0
Satyrn commented: ahh crafty. I love this idea but I would hate reviewing code that did this ><. This isn't Python! +0
Labdabeta 182 Posting Pro in Training Featured Poster

I find that nobody really uses preprocessor properly (aside from include statements of course). In fact they can almost always be removed completely since most IDEs have solid search/replace functionality (which is basically all that the define statement is). It is a lot faster to notice that you are typing a few things repeatedly, plug in an identifier for it (I usually use invalid C++ syntax) then find and replace. Here is an example for a char by char hello world program:

source:

int main()
{
    //~1~~2~//copy and paste this 12 times!
    ~1~h~2~
    ~1~e~2~
    ~1~l~2~
    ~1~l~2~
    ~1~o~2~
    ~1~ ~2~
    ~1~w~2~
    ~1~o~2~
    ~1~r~2~
    ~1~l~2~
    ~1~d~2~
    return 0;
}

Then you just hit CTRL-R (typical short-cut for replace) and type in replace ~1~ with cout<<' and replace ~2~ with '; These are even more versatile in many ways than a preprocessor because they have no ties to syntax at all!

I do notice that find-replace lacks that functional ability of macros however... but MS Word has a sick find and replace (it can be function and self modifying!) it should not be too long before such search capabilies are made standard on most IDEs.

ravenous 266 Posting Pro in Training

In gereral it's a really bad idea to use macros with very short names, like n. It removes the posibility of ever using that symbol as a variable name in your code. I think it's generally a good idea to try and use longish upper-case names for macros.

Additionally, if you're not careful (as Deceptikon showed) it's really easy to make your code pretty much unreadable using too many macros. Making a macro to replace std::cin << is largely pointless, it doesn't really save you that much, but adds loads of confusion for anyone trying to (literally) decypher your code.

On the point of the loop macro. Obviously, this is a pointless, terrible implementation of this idea. However, if you want a reasonably good macro for doing this, then you could use BOOST_FOREACH to do a much better job.

#define can be useful for doing OS/architecture/configuration dependent stuff. For instance, VC++ requires exporting of DLLs if you want to use them in a different dll, but gcc does this by default:

#ifdef __GNUC__
    #define DLL_EXPORT
#else
    #define DLL_EXPORT __declspec( dllexport )
#endif

Then you can define functions that you want exporting in VC++ as

DLL_EXPORT void myFunc();

Obviously, you'd need much better compiler detection than shown here, but it's just an example :o)

moxy 0 Newbie Poster

Lotta hate here .. i guess the author of the thread didn't realize didn't realize "stuck-in-the-mud" can mean other things besides the irrational zealots of a certain politcian.
In engineering we once got things done .. now we gotta be sure we don't offend the immature and neurotic.

sepp2k 378 Practically a Master Poster

Lotta hate here

Calling statements like "that's a bad idea" hateful is a tad dramatic, don't you think?

In engineering we once got things done .. now we gotta be sure we don't offend the immature and neurotic.

Nobody's offended (or at least I'm not - I suppose I can't really speak for the other people participating in the thread (though I've surely seen no indication that they're offended)). We're just explaining why, in our estimation, the macros suggested by the OP are bad ideas.

If the worry that you might get (mostly) constructive feedback on your code is what's keeping you from getting things done these days, then ... honestly I don't really know how to finish this sentence.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.