• 14
• 12
• 9
• 10
• 9

Object to reference conversion (C4239)

This topic is 4171 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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.

Share on other sites
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.

Share on other sites
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.

Share on other sites
Quote:
 Original post by KestIs 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

Share on other sites
Quote:
 Original post by AbsolutionIf 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 scottdewaldYou need to change your prototype tovoid Function(const Vector &v, int, int)When you create the Vector inlined like that, it creates a constant object that can't be modified.

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.

Share on other sites
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.

Share on other sites
Quote:
Original post by Conner McCloud
Quote:
 Original post by KestIs 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.

Share on other sites
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

Share on other sites
And a boat-load of information on cost-correctness:

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