常量表達(dá)式(const expression)是指值不會(huì)改變并且在編譯過程中就能得到計(jì)算結(jié)果的表達(dá)式。字面值屬于常量表達(dá)式,用常量表達(dá)式初始化的const對(duì)象也是常量表達(dá)式。
只要有可能使用constexpr,就使用它。
(資料圖片僅供參考)
C++11中constexpr的使用:
constexpr是C++11中添加的一個(gè)特性,其主要思想是通過在編譯時(shí)而不是運(yùn)行時(shí)進(jìn)行計(jì)算來提高程序的性能,將時(shí)間花在編譯上,而在運(yùn)行時(shí)節(jié)省時(shí)間(類似于模版元編程)。
C++11規(guī)定,允許將變量聲明為constexpr類型以便由編譯器來驗(yàn)證變量的值是否是一個(gè)常量表達(dá)式。聲明為constexpr的變量一定是一個(gè)常量,而且必須用常量表達(dá)式初始化。
盡管不能使用普通函數(shù)作為constexpr變量的初始值,但C++11標(biāo)準(zhǔn)允許定義一種特殊的constexpr函數(shù)。這種函數(shù)應(yīng)該足夠簡(jiǎn)單以使得編譯時(shí)就可以計(jì)算其結(jié)果,這樣就能用constexpr函數(shù)去初始化constexpr變量了。
一般來說,如果你認(rèn)定變量是一個(gè)常量表達(dá)式,那就把它聲明成constexpr類型。
所有constexpr對(duì)象都是const對(duì)象,而并非所有的const對(duì)象都是constexpr對(duì)象。如果你想讓編譯器提供保證,讓變量擁有一個(gè)值,用于要求編譯期常量的語(yǔ)境,那么能達(dá)到這個(gè)目的的工具是constexpr,而非const。
constexpr函數(shù)是指能用于常量表達(dá)式的函數(shù)。定義constexpr函數(shù)的方法與其它函數(shù)類似,不過要遵循幾項(xiàng)約定:函數(shù)的返回類型及所有形參的類型都得是字面值類型,而且函數(shù)體中必須有且只有一條return語(yǔ)句。constexpr函數(shù)或構(gòu)造函數(shù)被隱式地指定為內(nèi)聯(lián)函數(shù)。
constexpr函數(shù)體內(nèi)也可以包含其它語(yǔ)句,只要這些語(yǔ)句在運(yùn)行時(shí)不執(zhí)行任何操作就行。例如,constexpr函數(shù)中可以有空語(yǔ)句、類型別名以及using聲明。
允許constexpr函數(shù)的返回值并非一個(gè)常量。constexpr函數(shù)不一定返回常量表達(dá)式。
和其它函數(shù)不一樣,內(nèi)聯(lián)函數(shù)和constexpr函數(shù)可以在程序中多次定義。不過,對(duì)于某個(gè)給定的內(nèi)聯(lián)函數(shù)或者constexpr函數(shù)來說,它的多個(gè)定義必須完全一致?;谶@個(gè)原因,內(nèi)聯(lián)函數(shù)和constexpr函數(shù)通常定義在頭文件中。
constexpr函數(shù)只能調(diào)用其它c(diǎn)onstexpr函數(shù),不能調(diào)用簡(jiǎn)單函數(shù)(simple function)。constexpr函數(shù)不應(yīng)該是void類型。constexpr函數(shù)中不允許有前綴增量(++i),在C++14中已刪除此限制。
constexpr函數(shù)的理解:
(1).constexpr函數(shù)可以用在要求編譯期常量的語(yǔ)境中。在這樣的語(yǔ)境中,若你傳給一個(gè)constexpr函數(shù)的實(shí)參值是在編譯期已知的,則結(jié)果也會(huì)在編譯期間計(jì)算出來。如果任何一個(gè)實(shí)參值在編譯期未知,則你的代碼將無(wú)法通過編譯。
(2).在調(diào)用constexpr函數(shù)時(shí),若傳入的值有一個(gè)或多個(gè)在編譯期未知,則它的運(yùn)作方式和普通函數(shù)無(wú)異,亦即它也是在運(yùn)行期執(zhí)行結(jié)果的計(jì)算。這意味著,如果函數(shù)執(zhí)行的是同樣的操作,僅僅應(yīng)用的語(yǔ)境一個(gè)是要求編譯期常量的,一個(gè)是用于所有其它值的話,那就不必寫兩個(gè)函數(shù)。constexpr函數(shù)就可以同時(shí)滿足所有需求。
constexpr函數(shù)僅限于傳入和返回字面類型(literal type),意思就是這樣的類型能夠持有編譯期可以決議的值。在C++11中,所有的內(nèi)建類型,除了void,都符合這個(gè)條件。但是用戶自定義類型同樣可能也是字面類型,因?yàn)樗臉?gòu)造函數(shù)和其它成員函數(shù)可能也是constexpr函數(shù)。
在C++11中,constexpr函數(shù)都隱式地被聲明為const。
以下為測(cè)試代碼:
namespace {// constexpr function: constexpr函數(shù)被隱式地指定為內(nèi)聯(lián)函數(shù)constexpr int new_sz() { return 42; }constexpr size_t scale(size_t cnt) { return new_sz() * cnt; }constexpr int product(int x, int y) { return (x * y); }// pow前面寫的那個(gè)constexpr并不表明pow要返回一個(gè)const值,它表明的是如果base和exp是編譯期常量,pow的返回結(jié)果// 就可以當(dāng)一個(gè)編譯期常量使用;如果base和exp中有一個(gè)不是編譯期常量,則pow的返回結(jié)果就將在執(zhí)行期計(jì)算constexpr int pow(int base, int exp) noexcept{return (exp == 0 ? 1 : base * pow(base, exp - 1));}} // namespaceint test_constexpr_1(){// constexpr variablesconstexpr int mf = 20; // 20 is a constant expressionconstexpr int limit = mf + 1; // mf + 1 is a constant expressionconstexpr int foo = new_sz(); // foo is a constant expressionstd::cout << "foo:" << foo << "\n"; // foo:42// 當(dāng)scale的實(shí)參是常量表達(dá)式時(shí),它的返回值也是常量表達(dá)式;反之則不然int arr[scale(2)]; // okint i = 2;//int a2[scale(i)]; // error: scale(i)不是常量表達(dá)式size_t value = scale(i); // ok,constexpr函數(shù)不一定返回常量表達(dá)式std::cout << "value:" << value << "\n"; // value:84int sz = 1;//constexpr auto array_size = sz; // error, sz的值在編譯期未知const auto array_size1 = sz; // ok, array_size1是sz的一個(gè)const副本int arr2[product(2, 3)] = { 1, 2, 3, 4, 5, 6 };std::cout << "arr2[5]:" << arr2[5] << "\n"; // arr2[5]:6return 0;}
constexpr構(gòu)造函數(shù):盡管構(gòu)造函數(shù)不能是const的,但是字面值常量類的構(gòu)造函數(shù)可以是constexpr函數(shù)。事實(shí)上,一個(gè)字面值常量類必須至少提供一個(gè)constexpr構(gòu)造函數(shù)。
constexpr構(gòu)造函數(shù)可以聲明成=default的形式(或者是刪除函數(shù)的形式=delete)。否則,constexpr構(gòu)造函數(shù)就必須既符合構(gòu)造函數(shù)的要求(意味著不能包含返回語(yǔ)句),又符合constexpr函數(shù)的要求(意味著它能擁有的唯一可執(zhí)行語(yǔ)句就是返回語(yǔ)句)。綜合這兩點(diǎn)可知,constexpr構(gòu)造函數(shù)體一般來說應(yīng)該是空的。我們通過前置關(guān)鍵字constexpr就可以聲明一個(gè)constexpr構(gòu)造函數(shù)了。
constexpr構(gòu)造函數(shù)必須初始化所有數(shù)據(jù)成員,初始值或者使用constexpr構(gòu)造函數(shù),或者是一條常量表達(dá)式。
constexpr構(gòu)造函數(shù)用于生成constexpr對(duì)象以及constexpr函數(shù)的參數(shù)或返回類型。
以下為測(cè)試代碼:
namespace {class Debug {public:// constexpr構(gòu)造函數(shù)必須初始化所有數(shù)據(jù)成員constexpr Debug(bool b = true) noexcept : hw_(b), io_(b), other_(b) {}constexpr Debug(bool h, bool i, bool o) noexcept : hw_(h), io_(i), other_(o) {}constexpr bool any() const noexcept { return hw_ || io_ || other_; }constexpr bool get_hw() const noexcept { return hw_; }constexpr bool get_io() const noexcept { return io_; }constexpr bool get_other() const noexcept { return other_; }void set_hw(bool b) noexcept { hw_ = b; }void set_io(bool b) noexcept { io_ = b; }void set_other(bool b) noexcept { other_ = b; }//constexpr void set_hw(bool b) noexcept { hw_ = b; } // C++14//constexpr void set_io(bool b) noexcept { io_ = b; }//constexpr void set_other(bool b) noexcept { other_ = b; }private:bool hw_, io_, other_;};constexpr Debug hw_debug(const Debug& d1, const Debug& d2) noexcept{return d1.get_hw() && d2.get_hw(); // 調(diào)用constexpr成員函數(shù)}} // namespaceint test_constexpr_2(){constexpr Debug debug(false, true, false);if (debug.any())std::cout << "any true" << std::endl; // will outputif (debug.get_io())std::cout << "get_io true" << "\n"; // will outputconstexpr Debug prod(false);if (prod.any())std::cout << "any true" << std::endl; // will not outputconstexpr auto hw = hw_debug(debug, prod); // 使用constexpr函數(shù)的結(jié)果來初始化constexpr對(duì)象std::cout << "hw:" << hw.get_hw() << "\n"; // hw:0return 0;}
注:以上內(nèi)容主要整理自:《C++ Primer Fifth Edition》、《Effective Modern C++》
C++14中constexpr的使用:
在C++11中,constexpr函數(shù)只能包含一組非常有限的語(yǔ)法,包括但不限于:typedefs、using和一條返回語(yǔ)句。在C++14中,允許的語(yǔ)法集大大擴(kuò)展,包括最常見的語(yǔ)法,如if語(yǔ)句、多次返回、while或for循環(huán)等。
以下為測(cè)試代碼:
namespace {// C++14 constexpr functions may use local variables and loopsconstexpr int pow2(int base, int exp) noexcept{auto result = 1;for (int i = 0; i < exp; ++i) result *= base;return result;}constexpr unsigned int factorial(unsigned int n) {if (n <= 1)return 1;elsereturn n * factorial(n - 1);}} // namespaceint test_constexpr_14_1(){constexpr auto value = pow2(2, 4);std::cout << "pow2 value:" << value << "\n"; // pow2 value:16constexpr auto value2 = factorial(5);std::cout << "factorial value:" << value2 << "\n"; // factorial value:120return 0;}
執(zhí)行結(jié)果如下:
GitHub:https://github.com/fengbingchun/Messy_Test
關(guān)鍵詞: