__if_exists is a microsoft specific keyword for testing existence of identifiers at compile time:
It can come in very handy at “faked” template specialization as in some cases it provides a really much more simple, readable and better performing way than other methods like “real” specialization or overloading or whatever.
But now I have to port a big project to gnu c++ and I think I would start a little bit of crying if I would have to find other ways for the (admittedly few) occasions I used it
That’s a crappy keyword in my opinion…
Unfortunately, it doesn’t exist in gcc as far as I know, but then I may simply not know about it.
The proper C++ way to handle this is through the use of Concepts, ie adapt the operations carried on the type depending on some requirements.
Usually, it’s carried out with
traitsrather than real concepts, because it’s easier to put in place:And then you dump-enable your types by specializing the
has_dumpstructure.The simplest is to define 3 methods, one to route, the two others to execute the different branches:
Another use of the type traits would be in conjunction with the
enable_iffacilities:Here, instead of a runtime error message, you can get a compile-time error if the type does not have
has_dumpenabled, not sure if that’s you want.However both those methods are quite cumbersome, since the detection isn’t automated. That is why there is the Boost.Concept library.
The idea is that the check will be performed by a Concept object, created to test the requirements, and thus you don’t have to specialize traits any longer, which make easier on the development. However I have always found the documentation of Boost.Concept to be somewhat lacking.