Jump to content
  • Advertisement
Sign in to follow this  
Kest

Object to reference conversion (C4239)

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


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

Share this post


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

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 this post


Link to post
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 this post


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

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 this post


Link to post
Share on other sites
Guest Anonymous Poster
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 this post


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

Share this post


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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!