Why not allways use a const reference?

Started by
24 comments, last by _the_phantom_ 19 years, 4 months ago
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?
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.
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?
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.

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!
compilers may do it for you.

Sone languages do it automatically.
The joy of C++.
So if I don't care about consts in my code, or I could use a define for it, it will do SOMETHING?
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.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
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

This topic is closed to new replies.

Advertisement