R
Roal Zanazzi
Hi everyone,
I've accidentally found something in my C++ code that is definitively weird.
It is obviously an error in my code, but it is not signalled by
Microsoft Visual C++ 2005 compiler, not even with a warning.
While Borland TurboC++ 2006 compiler stop at it as an error
(E2034 Cannot convert 'bool' to 'int *').
// Code start here //
#include <iostream>
void func(int* p1, bool p2 = false)
{
std::cout << p1 << ", " << p2 << std::endl;
}
int main(int argc, char* argv[])
{
func(false); // <<< PROBLEM!
return 0;
}
// Code end here //
What I was doing is trying to pass the second parameter explicitely
(even though it has the same value as the default one),
forgetting the first parameter (think about someone else changed the
function signature without telling you).
I'm curious about what the standard says about this situation.
Do the standard mandate an implicit cast from false to 0 (zero)?
Should the compiler issue an error, at least a warning or let it go?
If I replace the "offending line" with:
func(true); // <<< PROBLEM!
in this case also the Microsoft compiler issues an error
(C2664: 'func' : cannot convert parameter 1 from 'bool' to 'int *'),
the same one given by Borland compiler.
TIA
I've accidentally found something in my C++ code that is definitively weird.
It is obviously an error in my code, but it is not signalled by
Microsoft Visual C++ 2005 compiler, not even with a warning.
While Borland TurboC++ 2006 compiler stop at it as an error
(E2034 Cannot convert 'bool' to 'int *').
// Code start here //
#include <iostream>
void func(int* p1, bool p2 = false)
{
std::cout << p1 << ", " << p2 << std::endl;
}
int main(int argc, char* argv[])
{
func(false); // <<< PROBLEM!
return 0;
}
// Code end here //
What I was doing is trying to pass the second parameter explicitely
(even though it has the same value as the default one),
forgetting the first parameter (think about someone else changed the
function signature without telling you).
I'm curious about what the standard says about this situation.
Do the standard mandate an implicit cast from false to 0 (zero)?
Should the compiler issue an error, at least a warning or let it go?
If I replace the "offending line" with:
func(true); // <<< PROBLEM!
in this case also the Microsoft compiler issues an error
(C2664: 'func' : cannot convert parameter 1 from 'bool' to 'int *'),
the same one given by Borland compiler.
TIA