Obscure c++ features

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
bbguimaraes
Chaos Rift Junior
Chaos Rift Junior
Posts: 294
Joined: Wed Apr 11, 2012 4:34 pm
Programming Language of Choice: c++
Location: Brazil
Contact:

Obscure c++ features

Post by bbguimaraes »

So I came across this post today and read the "Redefining keywords section". I thought to myself: "No way, this just can't be true". But behold, it is valid gcc4.6 code:
#define private public
class A { private: int a; };
int main() { return A().a; }
$ g++ --version; g++ -o test test.cpp && ./test; echo $?
g++ (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3
Copyright (C) 2011 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

0
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Obscure c++ features

Post by dandymcgee »

Funny, I saw someone use that exact #define when reading a post yesterday. Honestly, it doesn't surprise me.
I would never use such an ugly hack in real code though.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: Obscure c++ features

Post by Falco Girgis »

Yep, the preprocessor is ignorant of C/++ reserved words... Please, god, don't do this...

edit: btw, this is technically a C feature. And it's not really a feature. It's just a side-effect of the separation between preprocessor and compiler.
Post Reply