Object to reference conversion (C4239)
I just cranked my compiler up from level 3 to level 4 warnings, and there's a lot of work here. One of the warnings pertain to something that I've really relied on:
warning C4239: nonstandard extension used : 'argument' : conversion from 'Class' to 'Class &'.
This is generated from something like..
Function( Vector(0,0,1), 2, 1 );
..where the compiler wants..
Vector temp(0,0,1);
Function( temp, 2, 1 );
Is it safe to ignore this warning? Or should I break out my chainsaw?
Just looking for opinions.
If it is nonstandard I think your only worry is portability to other compilers and OS's. I would prefer the second option because of that and because that's what the compiler is doing for you anyway.
You need to change your prototype to
void Function(const Vector &v, int, int)
When you create the Vector inlined like that, it creates a constant object that can't be modified.
Quote:Original post by Kest
Is it safe to ignore this warning?
It is rather rare that you want to ignore any warnings. If they could be ignored, the compiler probably wouldn't bother warning you about them.
CM
Quote:Original post by Absolution
If it is nonstandard I think your only worry is portability to other compilers and OS's. I would prefer the second option because of that and because that's what the compiler is doing for you anyway.
I don't mind typing it out. I was just a little worried that one of the billion places I rely on this working won't be as simple.
Quote:Original post by scottdewald
You need to change your prototype toWhen you create the Vector inlined like that, it creates a constant object that can't be modified.void Function(const Vector &v, int, int)
My vector class does work that way. I invented the example code. But I didn't realize the const would resolve this issue. That means the majority of my code will be okay, then.
My real warning code for this situation used a class that works like a universal file-source. When objects are loaded, instead of accepting a filename, they accept a "plug". They load data from the plug, and the plug can pull that data from different sources. So my real code looks like this..
void Class::Create(Plug &plug, const Buffer *buf);
void Class::Create(const char *filename, const Buffer *buf) { return Create( Plug( filename ), buf ); }
But knowing the const rule is a big help. I don't mind declaring outside of function params for only non-const objects. Thanks for that.
You have non-standard compliant code, temporaries and literals can only be bound to constant references, so if you still want to pass temporaries/literals directly to functions use a constant reference or pass by value.
Quote:Original post by Conner McCloudQuote:Original post by Kest
Is it safe to ignore this warning?
It is rather rare that you want to ignore any warnings. If they could be ignored, the compiler probably wouldn't bother warning you about them.
CM
As far as I understand it, the functionality itself is safe, isn't it? The object becomes invalid once the function call returns. It's impossible to access it afterwards, because it has no handle.
As scottdewald pointed out, compiler complains because your code is not const-correct. You pass constant object to a function where non constant object is expected. Natural response from compiler is to create non static copy on the stack(so code works) a issue warning because of this.
MaR
MaR
And a boat-load of information on cost-correctness:
http://www.gotw.ca/publications/advice98.htm
http://www.parashift.com/c++-faq-lite/const-correctness.html
http://www.possibility.com/Cpp/const.html
http://www.linuxjournal.com/article/7629
http://www.cprogramming.com/tutorial/const_correctness.html
http://www.gotw.ca/publications/advice98.htm
http://www.parashift.com/c++-faq-lite/const-correctness.html
http://www.possibility.com/Cpp/const.html
http://www.linuxjournal.com/article/7629
http://www.cprogramming.com/tutorial/const_correctness.html
Quote:Original post by nobodynews
And a boat-load of information on cost-correctness:
http://www.gotw.ca/publications/advice98.htm
http://www.parashift.com/c++-faq-lite/const-correctness.html
http://www.possibility.com/Cpp/const.html
http://www.linuxjournal.com/article/7629
http://www.cprogramming.com/tutorial/const_correctness.html
The functionality of the object would be impossible in a const state with my situation. The plug object can read from buffers in memory, files, and other locations. It needs to record a read-offset in the memory-source cases.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement