I have a custom two dimensional point type, which has a template cast operator:
struct MyPoint
{
double x, y;
template < typename T >
operator T() const
{
return T{ x, y };
}
};
All works fine for std
types:
auto p1 = MyPoint{ 1, 2 };
auto p2 = static_cast< std::array< double, 2 > >( p1 );
auto p3 = static_cast< std::pair< double, double > >( p1 );
But if I try the same thing with QPointF
, I get this error (using g++ v4.8):
../CppTest/main.cpp:23:42: error: call of overloaded 'QPointF(MyPoint&)' is ambiguous
auto p3 = static_cast< QPointF >( p1 );
^
../CppTest/main.cpp:23:42: note: candidates are:
In file included from /usr/include/qt5/QtCore/QPointF:1:0,
from ../CppTest/main.cpp:2:
/usr/include/qt5/QtCore/qpoint.h:270:18: note: constexpr QPointF::QPointF(const QPoint&)
Q_DECL_CONSTEXPR inline QPointF::QPointF(const QPoint &p) : xp(p.x()), yp(p.y()) { }
^
/usr/include/qt5/QtCore/qpoint.h:205:46: note: constexpr QPointF::QPointF(const QPointF&)
class Q_CORE_EXPORT QPointF
^
/usr/include/qt5/QtCore/qpoint.h:205:46: note: constexpr QPointF::QPointF(QPointF&&)
It's like the compiler isn't even attempting to use the cast operator. If I change to an implicit conversion, such as:
QPointF p3 = p1;
It works fine. It also works if I use QPoint
- it just seems to be QPointF
that is causing problems, and I have no idea why.