|
|
|
|
||||||
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
I keep running across that I'm maintaining that likes to define
function parameters as "const char &" or "const int &", etc. Ie, constant reference parameters to a primitive type. This is for normal functions, not operators. I keep changing these to just have the plain old type, which is more efficient (I'm using embedded systems) and less obtuse. I'm puzzled why this one programmer insisted on odd style everywhere. Maybe he's just applying a template without thinking, as if it were like "const BigStruct &". But I'm wondering if there's some school of thought out there that encourages this and I'm missing something subtle. -- Darin Johnson |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
Darin Johnson wrote:
> I keep changing these to just have the plain old type, which > is more efficient (I'm using embedded systems) Did you check that your compiler is not already doing that automatically for you? |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
Juha Nieminen wrote:
> Darin Johnson wrote: >> I keep changing these to just have the plain old type, which >> is more efficient (I'm using embedded systems) > > Did you check that your compiler is not already doing that > automatically for you? If the function is expanded inline, this is highly likely, but for other functions OTOH, it's rather not likely. |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
Rolf Magnus wrote:
> Juha Nieminen wrote: > >> Darin Johnson wrote: >>> I keep changing these to just have the plain old type, which >>> is more efficient (I'm using embedded systems) >> Did you check that your compiler is not already doing that >> automatically for you? > > If the function is expanded inline, this is highly likely, but for other > functions OTOH, it's rather not likely. That's exactly what I found. I assume it's because the compiler does not yet know at the point of call whether the function (1) is defined in some other translation to expect its arguments pass-by-reference, or (2) will need to take the address of the argument. Still, Juha's question was a good one. |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
Darin Johnson wrote:
> I keep running across that I'm maintaining that likes to define > function parameters as "const char &" or "const int &", etc. > Ie, constant reference parameters to a primitive type. This is > for normal functions, not operators. > > I keep changing these to just have the plain old type, which > is more efficient Have you checked that, or are you just assuming? > (I'm using embedded systems) Excellent! If you really care about the low-level implementation of argument passing, you need to get comfortable with your platform's architecture and assembly language. It's not really on-topic here, but I'm guessing I'm not the only one here who has worked at that level. At worst, we can point you in the right direction. > and less obtuse. ITYM "confusing to me." > I'm puzzled why this one programmer insisted on > odd style everywhere. Maybe he's just applying a template > without thinking, as if it were like "const BigStruct &". He may indeed just want to pass arguments of primitive and user-defined types consistently. > But I'm wondering if there's some school of thought out > there that encourages this and I'm missing something subtle. Yes, there is. I have yet to see a compelling argument either for or against. |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
Darin Johnson wrote:
> I keep running across that I'm maintaining that likes to define > function parameters as "const char &" or "const int &", etc. > Ie, constant reference parameters to a primitive type. This is > for normal functions, not operators. > > I keep changing these to just have the plain old type, which > is more efficient (I'm using embedded systems) and less > obtuse. I'm puzzled why this one programmer insisted on > odd style everywhere. Maybe he's just applying a template > without thinking, as if it were like "const BigStruct &". > But I'm wondering if there's some school of thought out > there that encourages this and I'm missing something subtle. > > -- > Darin Johnson It was already implicitly mentioned but before changing make sure the functions do not use the address of the parameter; otherwise, the behavior may change. Barring that, I am not aware of any drawbacks of what you are doing -- as long as you know it gives your code an advantage on your particular platform. With the performance equal, I personally would still use by-value convention because, for a prospective code reader, it results in less tokens to scan and comprehend and less side effects to watch for. If a parameter type is a template parameter, however, the whole different can of worms should be considered. Unsure if it is relevant to your question. Hope this will -Pavel |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
On 2008-02-23 13:49:33, Pavel wrote:
> Darin Johnson wrote: >> I keep running across that I'm maintaining that likes to define function >> parameters as "const char &" or "const int &", etc. Ie, constant >> reference parameters to a primitive type. This is for normal >> functions, not operators. >> >> I keep changing these to just have the plain old type, which is more >> efficient (I'm using embedded systems) and less obtuse. I'm puzzled >> why this one programmer insisted on odd style everywhere. Maybe he's >> just applying a template without thinking, as if it were like "const >> BigStruct &". But I'm wondering if there's some school of thought out >> there that encourages this and I'm missing something subtle. > > It was already implicitly mentioned but before changing make sure the > functions do not use the address of the parameter; otherwise, the > behavior may change. Barring that, I am not aware of any drawbacks of > what you are doing -- as long as you know it gives your code an > advantage on your particular platform. Besides taking the address, there's always also the possibility to cast the const away, isn't there? This then may also change the behavior. Gerhard |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
Gerhard Fiedler <gelists@gmail.com> wrote:
> On 2008-02-23 13:49:33, Pavel wrote: > > Darin Johnson wrote: > >> I keep running across that I'm maintaining that likes to define function > >> parameters as "const char &" or "const int &", etc. Ie, constant > >> reference parameters to a primitive type. This is for normal > >> functions, not operators. > >> > >> I keep changing these to just have the plain old type, which is more > >> efficient (I'm using embedded systems) and less obtuse. I'm puzzled > >> why this one programmer insisted on odd style everywhere. Maybe he's > >> just applying a template without thinking, as if it were like "const > >> BigStruct &". But I'm wondering if there's some school of thought out > >> there that encourages this and I'm missing something subtle. > > > > It was already implicitly mentioned but before changing make sure the > > functions do not use the address of the parameter; otherwise, the > > behavior may change. Barring that, I am not aware of any drawbacks of > > what you are doing -- as long as you know it gives your code an > > advantage on your particular platform. > > Besides taking the address, there's always also the possibility to cast the > const away, isn't there? This then may also change the behavior. Taking an address of the parameter or casting away constness would both be dangerous... void foo( const char& c ) { // casting away constness would be undefined behavior and // taking (and presumably saving) the address would be pointless. } int main() { foo( '5' ); } |
|
![]() |
| Outils de la discussion | |
|
|