C++ Primer 读书笔记:(P295-324)

Function return values

Whether a function call is an lvalue depends on the return type of the function. Calls to functions that return references are lvalues; other return types yield rvalues.

// return an array of 10 ints
// note that array itself can't be returned, so return pointer
int arr[10]; // arr is an array of ten ints
int *p1[10]; // p1 is an array of ten pointers 
int (*p2)[10] = &arr; // p2 points to an array of ten ints

int (*func(int))[10] ...

constexpr again

Of the types we have used so far, the arithmetic, reference, and pointer types are literal types, the library IO and string types are not literal types.

A constexpr function is not required to return a constant expression.

The function will return a constant expression if its argument is a constant expression but not otherwise:

int arr[scale(2)]; // ok: scale(2) is a constant expression
int i = 2; // i is not a constant expression
int a2[scale(i)]; // error: scale(i) is not a constant expression

The return type and the type of each parameter in a must be a literal type, and the function body must contain exactly one return statement:

constexpr int new_sz() { return 42; }
constexpr int foo = new_sz(); // ok: foo is a constant expression

constexpr functions are implicitly inline.

Put inline and constexpr Functions in Header Files.

The NDEBUG Preprocessor Variable

The behavior of assert depends on the status of a preprocessor variable named NDEBUG. If NDEBUG is defined, assert does nothing. By default, NDEBUG is not defined, so, by default, assert performs a run-time check.

We can “turn off” debugging by providing a #define NDEBUGto define NDEBUG. Alternatively, most compilers provide a command-line option that lets us define preprocessor variables.

In addition to using assert, we can write our own conditional debugging code using NDEBUG. If NDEBUG is not defined, the code between the #ifndef and the #endif is executed. If NDEBUG is defined, that code is ignored:

void print(const int ia[], size_t size)
{#ifndef NDEBUG
// _ _func_ _ is a local static defined by the compiler that holds the function's name
cerr << _ _func_ _ << ": array size is " << size << endl; 
#endif
// ...

你可能感兴趣的:(C++ Primer 读书笔记:(P295-324))