File: staticassertions.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 (30 lines) | stat: -rw-r--r-- 1,240 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
21
22
23
24
25
26
27
28
29
30
The hi(static_assert)
        verb(    static_assert(constant expression, error message))

utility is available to allow assertions to be made from inside template
definitions. Here are two examples of its use:
        verb(    static_assert(BUFSIZE1 == BUFSIZE2,
                                "BUFSIZE1 and BUFSIZE2 must be equal");

    template <typename Type1, typename Type2>
    void rawswap(Type1 &type1, Type2 &type2)
    {
        static_assert(sizeof(Type1) == sizeof(Type2),
                        "rawswap: Type1 and Type2 must have equal sizes");
        // ...
    })

The first example shows how to avoid yet another preprocessor directive
        hi(preprocessor directive: error vs. static_assert)
 (in this case the ti(#error) directive).

The second example shows how tt(static_assert) can be used to ensure that
a template operates under the right condition(s).

The string defined in tt(static_assert)'s second argument is displayed and
compilation stops if the condition specified in tt(static_assert)'s first
argument is tt(false).

Like the tt(#error) preprocessor directive tt(static_assert) is a
i(compile-time) matter that doesn't have any effect on the i(run-time)
efficiency of the code in which it is used.