Re: A problem with g++ compiler
On 02.11.2010 02:18, luca wrote:
suppose we have the following structure:
typedef struct mystruct_t
{
int v1, v2;
unsigned char *v3;
// constructor
mystruct_t( int p_v1, int p_v2, unsigned char *p_v3) : v1(p_v1),
v2(p_v2), v3(p_v3)
{
}
} mystruct_t;
and the following class:
class MyClass
{
public:
static myfunc(mystruct_t&m);
};
Somewhere in my code i have the following line:
// ..
MyClass::myfunc(mystruct_t(1, 2, NULL));
It compile with Visual C/C++ , but when i try to compile it with g++
(using linux Mandriva + Eclipse IDE), i get the following error:
no matching function for call to 'MyClass::myfunc(mystruct_t&) '
I can't really understand why g++ can't compile that line. I simply
construct on-the-fly a mystruct_t object and pass it to myfunc()
static method...
Any idea?
This is one of the most persistent non-conformity examples of the
MS compiler families. The code is ill-formed, because an attempt is done
to bind an rvalue (the temporary produced from the expression
mystruct_t(1, 2, NULL)) to an lvalue-reference to non-const. Every
compiler has to reject this code when compiling in conforming mode.
Disable the Compiler "extensions" (/Za) of the Visual Studio compiler
and you should recognize that it will be rejected as well. This is one
of the examples how long-reaching effects pre-standard rules can have on
todays compilers.
Among the hundreds of discussions of this situation you might find the
following one helpful:
http://preview.tinyurl.com/2uulb8v
Solutions: If you do not intent to modify the argument within myfunc,
use the parameter type 'const mystruct_t&', otherwise create a normal
variable and provide the corresponding lvalue to the function. From
C++0x on an alternative solution would be to use an rvalue-reference as
parameter type.
Interestingly this question is not part of the FAQ (or I could not
find it). The nearest one I found was:
http://www.parashift.com/c++-faq-lite/const-correctness.html#faq-18.1
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! ]