tlbuild: Const

 
 8.2 Const
 =========
 
 The 'const' feature of C is valuable, but easy to mis-use.
 
 Function parameters
 ...................
 
 Ideally, a function parameter not modified by the function should be
 declared as 'const'.  This is important in particular for strings
 ('char*') because the actual arguments are often string literals.  It is
 perfectly legitimate and safe to use a type 'char*' value for a type
 'const char*' variable (in an assignment, as initializer, as function
 argument, or as return value).  It is equally safe to use a type
 'char**' value for a type 'const char*const*' variable, but not for a
 type 'const char**' variable since that might cause modification of a
 quantity supposed to be constant.
 
    Getting all 'const' qualifiers right can get quite involved but can
 almost always be done.  There are only a couple notable exceptions: the
 X11 headers are full of declarations that ought to use 'const' but do
 not; at one time, 'libfreetype' also did not fully specify 'const', but
 this has not been checked recently.
 
 What must be avoided with 'const'
 .................................
 
 The GCC compiler warnings "assignment discards qualifiers..." and
 analogous warnings for "initialization", "passing arg", or "return" must
 be strenuously avoided in our own code.  The only exception is when they
 are caused by X11 declarations or other third party code.
 
 What should be avoided with 'const'
 ...................................
 
 A type cast, e.g., from 'const char*' to 'char*' does not solve any
 problems; depending on warning options, it may only hide them.
 Therefore such casts should be avoided whenever possible and otherwise
 must be carefully analyzed to make sure that they cannot cause the
 modification of quantities supposed to be constant.