I've hit a wall concerning this explicit copy constructor issue. I've been writing a class to figure things out:
#include <iostream>
template<class T>
class Mat
{
private:
T data;
public:
void set(T value)
{
data = value;
}
Mat()
: data(T(0))
{
}
explicit Mat(const Mat& another)
{
*this = another;
}
Mat& operator=(const Mat& another)
{
data = another.data;
return *this;
}
template<class U>
explicit operator Mat<U>()
{
Mat<U> result;
result.set(static_cast<U>(data));
return result;
}
void print()
{
std::cout << data << std::endl;
}
};
int main()
{
Mat< double > d1;
d1.set(3.14159);
Mat< int > i1(static_cast<Mat<int>>(d1));
d1.print();
i1.print();
std::cin.sync();
std::cin.ignore();
return 0;
}
I want my copy constructor to take only explicitly converted instances of another object, so I declared it explicit, but now I get the error error "C2558: class 'Mat' : no copy constructor available or copy constructor is declared 'explicit'", even though I made an explicit cast:
static_cast<Mat<int>>(d1)
I've declared the copy constructor explicit because I want this to be illegal:
Mat<float> a;
Mat<int> b(a);
While, I would like the following to remain legal:
Mat<float> a;
Mat<int> b(static_cast<Mat<int>>(a));
EDIT: I've been tinkering with this concepts trying to define exactly what I want to get, and I seem to get some funny results:
#include <iostream>
class MatB
{
private:
float data;
public:
MatB()
:data(0.0f)
{
}
void set(float value)
{
data = value;
}
float getData() const
{
return data;
}
void print()
{
std::cout << data << std::endl;
}
};
class MatA
{
private:
double data;
public:
MatA()
:data(0.0)
{
}
void set(double value)
{
data = value;
}
double getData() const
{
return data;
}
explicit operator MatB()
{
MatB temp;
temp.set(static_cast<float>(getData()));
return temp;
}
void print()
{
std::cout << data << std::endl;
}
};
class MatC
{
private:
int data;
public:
MatC()
:data(0)
{
}
explicit MatC(const MatB& in)
:data(static_cast<int>(in.getData()))
{
}
void print()
{
std::cout << data << std::endl;
}
};
int main()
{
MatA someA;
someA.set(3.14159);
MatC constructCFromA(someA);
someA.print();
constructCFromA.print();
std::cin.sync();
std::cin.ignore();
return 0;
}
In this example, constructCFromA(someA) shouldn't compile (imo) - even the linker marks it as an error(VS2013), still it compiles just fine... I am not sure whether my understanding of 'explicit' is incorrect, whether the IDE marks it as an error incorrectly, or the compiler compiles it even though it shouldn't. I thought I would need to do something like this:
constructCFromA(static_cast<MatB>(someA));
The IDE seems to agree with me, but the compiler doesn't. I must say I am pretty confused.
EDIT2: Never mind, in Ideone it doesn't compile, so I guess MS are to blame. I think the 2nd code illustrates well the behaviour I want to get. Basically make non-explicit conversions at initialization and assignment illegal. It seems however, that making the copy constructor explicit has various "side-effects".