File: constcast.yo

package info (click to toggle)
c%2B%2B-annotations 13.02.02-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,576 kB
  • sloc: cpp: 25,297; makefile: 1,523; ansic: 165; sh: 126; perl: 90; fortran: 27
file content (20 lines) | stat: -rw-r--r-- 1,091 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
The tt(const) keyword has been given a special place in casting. Normally
anything tt(const) is tt(const) for a good reason. Nonetheless situations
may be encountered where the tt(const) can be ignored. For these special
situations the tt(const_cast) should be used. Its syntax is:
                     verb(        const_cast<type>(expression))

A ti(const_cast<type>(expression)) expression is used to undo the
tt(const) attribute of a (pointer) type.

    The need for a tt(const_cast) may occur in combination with functions from
the standard bf(C) library which traditionally weren't always as const-aware
as they should. A function tt(strfun(char *s)) might be available, performing
some operation on its tt(char *s) parameter without actually modifying the
characters pointed to by tt(s). Passing tt(char const hello[] = "hello";) to
tt(strfun) produces the warning
   verb(        passing `const char *' as argument 1 of `fun(char *)' discards const)

A tt(const_cast) is the appropriate way to prevent the warning:
                 verb(        strfun(const_cast<char *>(hello));)