mercredi 28 juin 2017

Call fallback method if method does not exist

I want to be able to do the following in C++

  1. Try to call a function from a namespace, e.g., boost::filesystem::copy
  2. If copy is not a member of boost::filesystem, call a fallback function, e.g., boost::filesystem3::copy
  3. If fallback function does not exist (this could be either because boost does not have member filesystem3, or because boost::filesystem3 does not have member copy), the code should not compile.

After reading loads and loads of extremely long and complicated pieces of code, it is unclear to me what the simple way of doing this would be. A C++11 solution is fine. But the code sometimes requires to compile with an old boost version (1.39.0), which is precisely why this workaround is required.

Currently I do it by creating method alias after checking the BOOST_VERSION macro. But it would be good to know of a more sophisticated alternative that could be applicable for more general cases.

Aucun commentaire:

Enregistrer un commentaire