samedi 28 décembre 2019

Is there any way to change function prototypes at compile time in C++?

suppose you have to write code that should compile under C++,C++11,C++17,etc.

A function like this for example.

bool Ispalindrome(const std::string &str) 
{ 
   // Code 
}

That compiles under all C++ implementations. But if you want to use the old and the new the C++17 string_view feature you have deal with something similar to

#ifdef LEGACYCPP
   bool Ispalindrome(const std::string &str) 
  { 
     // Code 
  }
#elseif CPP17
  bool Ispalindrome(std::string_view str) 
  { 
     // OMG Repeated Code 
  }
#endif

Using conditional compiling is right, but have to repeat code.

Is there any way to choose a function prototype at compile time ? Or other way to circumvent double coding ? (in situations where it can be applied)

Thanks

Aucun commentaire:

Enregistrer un commentaire