When do I use __if_exists without writing tons of crappy code?
Looks like this keyword is like C preprocessor directive, but is processed after preprocessor. And IntelliSense doesn’t parse it and highlight code as dead or alive. These together make analysis of code written with __if_exists really non-trivial.
So far I found only one relatively safe case. We have a container class that takes an address of stored object. When a class stored has an overloaded operator& that overloaded operator is called and this causes problems.
So I added the following check:
__if_exists( T::operator& ) {
static_assert( false );
}
and now the code at least won’t compile if there’s an operator& member function is the type stored.
IMO this use case is quite clear and easy to read.
What other cases are there of using __if_exists without getting tons of unreadable code?
I think you can use it to distinguish unions and classes, since classes do have constructors and unions don’t.
You’d want this in e.g.
boost::type_traits::is_class<T>andboost::type_traits::is_union<T>