Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #6797

Re: Use of const for non-reference parameters

From "Fred Zwarts \(KVI\)" <F.Zwarts@KVI.nl>
Newsgroups comp.lang.c++
Subject Re: Use of const for non-reference parameters
Date 2011-06-16 13:33 +0200
Organization albasani.net
Message-ID <itcpll$63k$1@news.albasani.net> (permalink)
References <ygfhb7qi2dg.fsf@janus.isnogud.escape.de>

Show all headers | View raw


"Urs Thuermann"  wrote in message 
news:ygfhb7qi2dg.fsf@janus.isnogud.escape.de...
>
>I'm working on a project that uses const for function parameters that
>are neither references nor pointers, i.e. something like this:
>
>int foo(const int a) { ... }
>int bar(const string b) { ... }
>
>I don't see why this is useful.  Since the argument is copied to the
>called function it doesn't matter to the caller whether the argument
>is modified or not.  So why would one want to write code like that?
>
>urs

As said already by others, it makes sense in the function body just like any 
other const declaration.
For prototype declarations it makes less sense.  It is superfluous (just 
like the a and the b).
Sometimes prototypes are generated automatically. Such generators may copy 
the const to the prototype. So that may be a reason why you see const in 
prototypes. Further, I have seen one compiler which issued a warning if the 
const was missing in the prototype, but present in the full function 
definition. That may be another reason why you see it in prototypes. 

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Use of const for non-reference parameters Urs Thuermann <urs@isnogud.escape.de> - 2011-06-16 10:53 +0200
  Re: Use of const for non-reference parameters Ian Collins <ian-news@hotmail.com> - 2011-06-16 21:15 +1200
    Re: Use of const for non-reference parameters yatremblay@bel1lin202.(none) (Yannick Tremblay) - 2011-06-16 11:50 +0000
  Re: Use of const for non-reference parameters "Balog Pal" <pasa@lib.hu> - 2011-06-16 11:35 +0200
    Re: Use of const for non-reference parameters Urs Thuermann <urs@isnogud.escape.de> - 2011-06-16 15:21 +0200
      Re: Use of const for non-reference parameters Marc <marc.glisse@gmail.com> - 2011-06-16 16:25 +0000
  Re: Use of const for non-reference parameters Michael DOUBEZ <michael.doubez@free.fr> - 2011-06-16 02:38 -0700
  Re: Use of const for non-reference parameters Saeed Amrollahi <amrollahi.saeed@gmail.com> - 2011-06-16 03:15 -0700
  Re: Use of const for non-reference parameters "Fred Zwarts \(KVI\)" <F.Zwarts@KVI.nl> - 2011-06-16 13:33 +0200
  Re: Use of const for non-reference parameters Howard Hinnant <howard.hinnant@gmail.com> - 2011-06-16 09:50 -0700
    Re: Use of const for non-reference parameters Michael DOUBEZ <michael.doubez@free.fr> - 2011-06-16 13:22 -0700
      Re: Use of const for non-reference parameters Howard Hinnant <howard.hinnant@gmail.com> - 2011-06-16 13:49 -0700
        Re: Use of const for non-reference parameters Gerhard Fiedler <gelists@gmail.com> - 2011-06-17 11:41 -0300
          Re: Use of const for non-reference parameters "Balog Pal" <pasa@lib.hu> - 2011-06-17 18:40 +0200
            Re: Use of const for non-reference parameters "Thomas J. Gritzan" <phygon_antispam@gmx.de> - 2011-06-17 22:01 +0200
              Re: Use of const for non-reference parameters "Balog Pal" <pasa@lib.hu> - 2011-06-18 03:01 +0200
            Re: Use of const for non-reference parameters Joshua Maurice <joshuamaurice@gmail.com> - 2011-06-17 13:30 -0700
          Re: Use of const for non-reference parameters Howard Hinnant <howard.hinnant@gmail.com> - 2011-06-17 16:51 -0700
  Re: Use of const for non-reference parameters "Johannes Schaub (litb)" <schaub.johannes@googlemail.com> - 2011-06-17 16:35 +0200
  Re: Use of const for non-reference parameters Juha Nieminen <nospam@thanks.invalid> - 2011-06-18 10:14 +0000
    Re: Use of const for non-reference parameters Jorgen Grahn <grahn+nntp@snipabacken.se> - 2011-06-18 11:45 +0000
      Re: Use of const for non-reference parameters "Alf P. Steinbach /Usenet" <alf.p.steinbach+usenet@gmail.com> - 2011-06-18 19:27 +0200

csiph-web