天天看點

c++11的constexpr關鍵字 literal(常量)

a constant expression is an expression whose value can’t change and that can be evaluated at a compile time. a literal is a constant expression.

whether a given object or expression is a constant expression depends on the types and the initializers. For example:

const int max_files = ; // max_files is a constant expresion
int staff_size = ; //staff_size is not a constant expression
const int sz = get_size(); //sz is not a constant expression
           

對于staff_size,盡管初始化值(initializer)是一個literal,但是它本身是一個plain int, not a const int,是以staff_size不是一個constant exprssion.

而對于sz來說,它的initializer的值在編譯的時候才能夠确定,是以sz不是一個constant expression.

in a large system, it can be difficult to determine for certain that an initializer is a constant expression.

under the new standard, we can ask the compiler to verify that a variable is a constant expression by declaring the variable in a constexpr declaration.

c++primer上舉的幾個例子是這樣

constexpr int mf = ; // 常量表達式  
constexpr int limit = mf + ; // 常量表達式  
constexpr int sz = size(); // 如果size()是常量表達式則編譯通過,否則報錯  
           

我自己試了一下:

int size()
{
        return -;
}
int main()
{
        constexpr int sz = size();
        return ;
} 
           
xiahuixia@xiahuixia-Inspiron-:~/c++/primercode$ g++ -o constexpr constexpr.cpp -std=c++0x
           

結果是:

constexpr.cpp: In function ‘int main()’:
constexpr.cpp::: error: call to non-constexpr function ‘int size()’
  constexpr int sz = size();
                          ^
           

再來看看這個literal這個英文的意思,有點晦澀

c++primer是這樣解釋的:

the types we can use in a constexpr are known as “literal types”

because they are simple enough to have literal values.

of the types we have used so far, the arithmetic, reference, and pointer types are literal types.

但是,問題來了,our self-defined class type and the library IO and string types are not literal types. hence, we can’t define variables of these types as constexprs.

也就是說我們自定義的類和IO庫還有字元串不是literal type類型。

為什麼呢?且拿我們自定義的類來說吧。

比如說

class a{
int i;
char c;
}
a my_a;
my_a.i = ....
my_a.c = ....
           

類a的成員值在聲明的時候或許根本沒有初始化。是以不能稱得上是literal type.(暫時我還不确定我的假設是不是正确的)