I'm trying to check if an operator exists at compile time, if it doesn't I just want it ignored, is there any way to do that?
example operator:
template <typename T>
QDataStream& operator<<(QDataStream& s, const QList<T>& l);
I'm trying to check if an operator exists at compile time, if it doesn't I just want it ignored, is there any way to do that?
example operator:
template <typename T>
QDataStream& operator<<(QDataStream& s, const QList<T>& l);
This is an old question but it's worth noting that Boost just added this ability for almost all operators with their newest Operator Type Traits. The specific operator OP asked about is tested with
boost:has_left_shift
.It isn't too easy and in C++03 it isn't in general possible. If you use
int*
andint*
forop<<
for example, you will get a hard error at compile time. So for non-class types, you need to filter out the types that the Standard forbids.For
op+
I once wrote such a thing for the kicks. Note that I'm usingC
headers, because I needed to test the code with theclang
compiler too, which at that time didn't support my C++ headers:Of course, it's very important to do tests afterwards
I ended up using a fallback namespace :
Also found the proper way to check for operators at compile time (although I'm going with the fallback namespace).
more or less based on this :
//edit changed has_stream_operators a bit.
//edit removed the link, apparently the site has some attack javascript.