Use #ifdef to do condition compilation - C++ Preprocessor

C++ examples for Preprocessor:Directive

Description

Use #ifdef to do condition compilation

Demo Code

#include <string>
#include <iostream>
using std::string;

namespace print1 {
  // Function prototype
  void print(const string& s);
}

namespace print2 {
  // Function prototype
  void print(const string& s);
}


void print1::print(const string& s) {
  std::cout << s << " (Namespace name is print1.) " << std::endl;
}

void print2::print(const string& s) {
  std::cout << s << " (Namespace name is print2.) " << std::endl;
}
void print_that(const string& s) {
  print2::print(s);//from ww  w .ja v a  2 s  .c  o  m
}


void print_this(const string& s) {
  print1::print(s);
}

#define DO_THIS

#ifdef DO_THIS
#define PRINT(val) print_this(#val);
#else
#define PRINT(val) print_that(#val);
#endif

int main()
{
#ifdef DO_THIS
  print_this("This is a test string using printthis().");
#else
  print_that("This is a test string using printthat().");
#endif
}

Result


Related Tutorials