Hello, all.
The following code results in a C2666 error (2 overloads have similar
conversions).
class FSVec2D
{
public:
FSVec2D()
{ // code omitted
}
FSVec2D(double first, ...)
{
va_list args;
va_start(args, first);
double d = first;
for (int i = 0; i < 2; ++i)
{
_data[i] = d;
d = va_arg(args, double);
}
va_end(args);
}
FSVec2D(const FSVec2D& _r, double _w)
{
_data[0] = _r[0];
_data[1] = _w;
}
double& operator[](int i)
{ return _data[i]; }
const double& operator[](int i) const
{ return _data[i]; }
private:
double _data[2];
};
FSVec2D v2(1.1, 2.2); // Error here
The two overloads that it sties are the default, compiler-generated
copy constructor and the constructor that takes a FSVec2D reference
and a double as parameters. Apparently, the intended constructor with
the variable parameters isn't even considered. This appears to me to
be a compiler bug. How can it convert a double to a class reference?
Am I making an erroneous assumption?
BTW, this code is a simplification of the original code. The
constructor above taking the reference and a double, of course,
doesn't make much sense. The original class is a parameterized class
as follows (notice the reference to a vector one size smaller). I
guess I could write an explicit specialization of the class for _Size
of 2 and eliminate the last constructor, since I have no use for a
vector of size 1. However, correct me if I'm wrong, I would have to
duplicate all of the rest of the functions in the class. Right?
template <size_t _Size, class _Type>
class FSVector
{
// Fixed-size vector
public:
FSVector();
FSVector(_Type first, ...);
FSVector(const FSVector<_Size - 1, _Type>& _r, _Type _w);
// ...
private:
_Type _data[_Size];
};
Other observations:
This error doesn't just happen with constructors. It also happens with
member functions and static member functions if one has the variable
parameter list and the other has the reference and primitive
parameters, as long as the reference is to a 'similar' class. By that
I mean, for the simplified sample (non-template) class above, it has
to be the same class. In the template class, the class reference isn't
to the exact same class, but uses the same template.
Interestingly, if I supply a copy-constructor (or in the case of
member functions), the compiler says there are 2 overloads that have
similar conversions, but only provides reference to the one with the
non-varible parameters. That is, it says "could be 'blah blah'" but
omits the "or 'blah blah blah'".
Thanks for any help or comments.
-John