Re: Strange convertion
Am 19.10.2012 22:04, schrieb Vianney Lan?on:
while compiling our code we found that a wrong constructor was called.
No, it isn't.
The code was something like that.
#include <iostream>
struct Toto
{
Toto(unsigned short a[2]){ x=a[0]; y=a[1];}
unsigned short x, y;
};
This constructor signature is a red herring. It is equivalent to the
following one:
Toto(unsigned short a*){ x=a[0]; y=a[1];}
thus you effectively have declared a constructor that accepts a pointer
value to unsigned short. This is one of the inheritances of C: Arrays
cannot be used as function parameters or function return types. If you
attempt to declare such a function parameter, it will silently be
considered as a pointer to the array's element type. Personally I find
the validity of this code quite unfortunate, it should better have been
made ill-formed (like attempting to declare an array type as return type).
void print(const Toto& toto)
{
std::cout<<toto.x<<" "<<toto.y<<std::endl;
}
int main()
{
print(false); // <= convert false to Toto
This works, because you are effectively providing a null pointer
constant to a function that accepts a pointer value. This is valid and
will simply call the function with a null pointer value.
return 0;
}
Because the constructor of Toto is not explicit it is normal that
unsigned short[4] can be converterd to Toto.
Try to provide an argument that is an array unsigned short[1] or
unsigned short[5] and you should see that it becomes accepted, too,
because all of these convert to unsigned short*. This is a very
dangerous thing, so better don't declare such a function. If you really
want to declare a function that only accepts an array, you need to use a
parameter of reference to arrayr type, such as
Toto(unsigned short (&a)[2]){ x=a[0]; y=a[1];}
or even
Toto(const unsigned short (&a)[2]){ x=a[0]; y=a[1];}
But I do not understand by what mechanisme the bool value false can be
convert into unsigned short[4].
See above, it is a null pointer constant accepted by a function taking a
pointer.
Because it doesn't compile if i change the value from false to true it
's probably because of some convertion from false to 0 literal.
The reason is, that 'true' is not a null pointer constant (The value is
equivalent to '1'). I should add here that the C++ committee is
currently considering to reduce the amount of numbers to specify such a
null pointer constant, because it causes also problems in templates.
Assuming the current suggestion, as described here:
http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#903
will be accepted, the value 'false' would no longer be an acceptable
null pointer value.
HTH & Greetings from Bremen,
Daniel Kr?gler
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]