你更換 const int one = 1;
const int two = 2;
這個? constexpr int one = 1;
constexpr int two = 2;
我的理解是正確的,這兩個塊具有相同的語義,它目前的味道只是一個事? 在另一方面,作爲constexpr意味着const,你可以認爲它是比較一致的總是傾向於更嚴格的形式,即使在平凡的情況下它不會有所作爲?
測試一個簡單的UTF8 strlen函數,是頗爲吃驚,樹幹鐺完全消除它(GCC不): static int strlenutf8(const char* s)
{
int i = 0, l = 0;
while (s[i])
{
if ((s[i] & 0xc0) != 0x80) l++;
l++;
}
return j;
我將我的代碼庫的許多位操作函數轉換爲constexpr。沒有真正的理由這樣做,主要是因爲'。 在C++ 11中編寫一個函數constexpr需要代碼是一個單一的表達式,這並不總是可能的(並且並不總是需要)。 無論如何,我有一些麻煩,使用此功能: //Given a binary value like 00000100011, fills every bit below the highest '1