Wrong results due to missing `explicit` on constructors
Here is a simple program using lib2geom 1.2.2 that should not and does not compile, because the multiplication operator for two Point
s is not defined:
#include <2geom/point.h>
//#include <2geom/transforms.h>
#include <iostream>
using namespace Geom;
int main()
{
std::cout << Point(2, 2) * Point(5, 5) << '\n';
}
But uncomment the commented-out include, and not only does it now compile, but it prints
(7, 7)
The problem is that including the transforms.h
header brings in Geom::Translate
, which has a single-argument constructor that is not marked explicit
:
class Translate
: public TransformOperations< Translate >
{
Point vec;
public:
...
/// Construct a translation from its vector.
Translate(Point const &p) : vec(p) {}
This breaks C.46: By default, declare single-argument constructors explicit, and leads to the unexpected result
a * b == a * Translate(b) == a + b
so that multiplication actually performs addition. This is insanity and needs to be fixed by adding explicit
.