0 Replies - 79 Views - Last Post: 10 August 2008 - 01:22 PM

#1 NickDMax   User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Preprocessor IIF

Posted 10 August 2008 - 01:22 PM

Description: Sometimes it is useful to have an inline IIF to use in the preprocessor rather than having to use #if.
#include <iostream>
using namespace std;

//Sometimes it is useful to have an inline IIF for the preprocessor
#define IIF(bit, t, f) IIF_ ## bit(t, f)
#define IIF_0(t, f) f
#define IIF_1(t, f) t

//The idea can be extended...
#define PPCHOICE(choice, ch1, ch2, ch3, ch4) PPCHOICE_ ## choice(ch1, ch2, ch3, ch4)
#define PPCHOICE_EN(ch1, ch2, ch3, ch4) ch1
#define PPCHOICE_FR(ch1, ch2, ch3, ch4) ch2
#define PPCHOICE_SP(ch1, ch2, ch3, ch4) ch3
#define PPCHOICE_GR(ch1, ch2, ch3, ch4) ch4

#define GREETING_EN "Hello World!"
#define GREETING_FR "Bonjour Monde!"
#define GREETING_SP "Hola Mundo!"
#define GREETING_GR "Hallo Welt!"

// I am not saying that this is the best way to do things
// only trying to give an example of how this may be used.
int main() {
	cout << IIF(0, "True", "False") << endl;
	cout << PPCHOICE(EN, GREETING_EN, GREETING_FR, GREETING_SP, GREETING_GR) << endl;
	cin.get();
	return 0;
}


Is This A Good Question/Topic? 0
  • +

Page 1 of 1