I am trying to use templates recursively to define (at compile-time) a d-tuple of doubles. The code below compiles fine with Visual Studio 2010, but g++ fails and complains that it “cannot call constructor ‘point<1>::point’ directly”.
Could anyone please shed some light on what is going on here?
Many thanks, Jo
#include <iostream>
#include <utility>
using namespace std;
template <const int N>
class point
{
private:
pair<double, point<N-1> > coordPointPair;
public:
point()
{
coordPointPair.first = 0;
coordPointPair.second.point<N-1>::point();
}
};
template<>
class point<1>
{
private:
double coord;
public:
point()
{
coord= 0;
}
};
int main()
{
point<5> myPoint;
return 0;
}
What are you trying to do with:
It looks like you want to explicitly call the
pointdefault constructor – which has already been called when thepairwas constructed. You cannot call constructors directly (unless you use placement new, which wouldn’t make sense in this scenario)Just remove that line.
If you for some reason want to overwrite the already constructed
.secondby assigning to it from a temporarypoint<N-1>you can do so withcoordPointPair.second = point<N-1>();.If you, for a more complex case, want to pass arguments to a
pointconstructor you can do this in the initializer list: