#13 Ambiguous overload with DBus::MessageIter's << operator

open
Andreas Volz
None
5
2014-12-31
2012-04-23
Chong Kai Xiong
No

In include/dbus-c++/types.h line 424, i.e.

eit << mit->first << mit->second;

the second call to the operator<< is ambiguous because the compiler has not seen the definition of

DBus::MessageIter &operator << (DBus::MessageIter &iter, const DBus::Variant &val);

Because DBus::Variant defines a generic conversion operator (template <typename T=""> operator T() const), the compiler is free to choose any of the operator<< overloads defined earlier on the file:

DBus::MessageIter &operator << (DBus::MessageIter &iter, const DBus::Invalid &)
DBus::MessageIter &operator << (DBus::MessageIter &iter, const uint8_t &val)
DBus::MessageIter &operator << (DBus::MessageIter &iter, const bool &val)
...

I'm not sure what the C++ standard says, but Clang barfs on this while GCC manages to apply the appropriate overload. The solution is to move the definition of the DBus::Variant overload to the top.

Discussion