Jump to content
  • Advertisement
Sign in to follow this  
the_cyberlord

Why not allways use a const reference?

This topic is 5028 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

Wouldn't that be a liiittle bit faster for functions to do that instead of just passing a normal argument? And another question: What exactly is an inline function? It's a sort of macro, isn't it?

Share this post


Link to post
Share on other sites
Advertisement
if your function's purpose is to modify its args, then you can't pass the args as const.
inline function is similar to macro in that their contents are copied intact and embeded in where they appear in the program. i.e. calling inline functions doesn't require the stack setup, arg retrieval, etc. steps for calling normal functions.

Share this post


Link to post
Share on other sites
But I said const reference, because so it only passes an address and no stack pushing is required,wich gives some speed, and because it's const, it acts like a normal pass-by value. Why doesn't everybody allways use refences then?

Share this post


Link to post
Share on other sites
Quote:
Original post by the_cyberlord
But I said const reference, because so it only passes an address and no stack pushing is required,wich gives some speed, and because it's const, it acts like a normal pass-by value. Why doesn't everybody allways use refences then?

That's what people usually do for user-defined types.

Share this post


Link to post
Share on other sites
Quote:
Original post by the_cyberlord
Wouldn't that be a liiittle bit faster for functions to do that instead of just passing a normal argument?


The main reason people don't is because:
1. Looks messy - when you have a huge program, you dont want it cluttered with "consts"
2. Time consuming - once again, when you are on large scale design, it's just a pain to make everything const, you never know when you will or will not need something to be const
3. Yes it would be faster - but only if you are passing in very large structs (>1kb) - the speed benefits for always passing in const int rather than int are along the lines of 1x10e100+ or so, so your not doing your self any good.
4. Most people don't understand the concept or just don't care, so they won't use it.

Quote:

And another question: What exactly is an inline function? It's a sort of macro, isn't it?


An inline function is a way to make a frequently called function execute more efficiently by telling the compiler to replace all instances of the function call with the code. Defining a function "inline" only **requests** the compiler to make it inline - there is no gaurntee. The ways you can make a function not be inline are - calling another function, using some sort of loop/switch statement, recursion. It is described as a type-safe macro - so it is similar to a macro in terms of what the compiler does with the code, but not the actual use of it.

I hope this helps!

Share this post


Link to post
Share on other sites
Quote:
Original post by the_cyberlord
So if I don't care about consts in my code, or I could use a define for it, it will do SOMETHING?
Yes.
It has three useful purposes.
1. It gives compilers more opportunity for optimisation. Some will, some won't compile various 'const' things better.
2. It helps make things clear to users of a function to know that a parameter is never modified so you know your supplied data is safe from changes by that function. (I would consider this the main reason)
3. It allows compilers spot mistakes in functions you write, where you might accidentally write code which would modify something it shouldn't.

Oh and of course it's only faster when the data type is not the same size as a pointer i.e. an int x is of course faster than a const int &x (assuming the compiler doesn't optimise it as thought it was int x to begin with).

An inline function is a request to the compiler to inline a function. Inlining means that the function's code is compiled into the function that calls it, thus totally eliminating any pushing and poping that might otherwise be required for calling it. This can make the function that calls it bigger, but for very small inlined functions, taking away the pushing and poping otherwise required actually makes it shorter.
It's still not usually quite as fast as putting the function's code as a hash-define though, which of course makes it truly compiled to be part of the caller's code.

Share this post


Link to post
Share on other sites
Quote:
Original post by Drew_Benton
3. Yes it would be faster - but only if you are passing in very large structs (>1kb) - the speed benefits for always passing in const int rather than int are along the lines of 1x10e100+ or so, so your not doing your self any good.


this is true only to a degree, but when you are calling a function in your programs main loop (ie, many times over), there will be an increase of:

(hypothetical amount) -> 1x10e100 * <number of times you call>

so if you call 100 times it will be 1x10e102, although this might not seem much, when you are looking for clocks, this may help you

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!