PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > why "const char &" as a parameter?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
why "const char &" as a parameter?

Réponse
 
LinkBack Outils de la discussion
Vieux 23/02/2008, 02h28   #1
Darin Johnson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut why "const char &" as a parameter?

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
  Réponse avec citation
Vieux 23/02/2008, 09h22   #2
Juha Nieminen
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: why "const char &" as a parameter?

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?
  Réponse avec citation
Vieux 23/02/2008, 10h39   #3
Rolf Magnus
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: why "const char &" as a parameter?

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.

  Réponse avec citation
Vieux 23/02/2008, 12h00   #4
Jeff Schwab
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: why "const char &" as a parameter?

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.
  Réponse avec citation
Vieux 23/02/2008, 12h06   #5
Jeff Schwab
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: why "const char &" as a parameter?

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.
  Réponse avec citation
Vieux 23/02/2008, 16h49   #6
Pavel
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: why "const char &" as a parameter?

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
  Réponse avec citation
Vieux 23/02/2008, 21h06   #7
Gerhard Fiedler
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: why "const char &" as a parameter?

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
  Réponse avec citation
Vieux 24/02/2008, 00h08   #8
Daniel T.
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: why "const char &" as a parameter?

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' );
}
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 11h39.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,20995 seconds with 16 queries