簡體   English   中英

Lambda錯誤的GCC行為

[英]Wrong gcc behaviour with lambda

通過編譯:

#include <iostream>
#include <sstream>

std::string makeList (std::string sep)
{
    auto makeItem = [&] (std::string item)
    {
        static char count = '0';
        return (++count, count) + sep + item + '\n';
    };

    return makeItem ("first") + makeItem ("second") + makeItem ("third");
}

int main() 
{
    std::cout << makeList (". ");
}

使用gcc(5.4.0,c ++ 11標志)的輸出是這樣的:

3. first
2. second
1. third

而clang(3.8,c ++ 11 flag)得到的正確輸出是:

1. first
2. second
3. third

這種行為是否有特定原因?

根據cppreference

幾乎所有C ++運算符的操作數求值順序(包括函數調用表達式中的函數自變量的求值順序以及任何表達式中子表達式的求值順序)均未指定。 編譯器可以按任何順序求值操作數,並且當再次求同一個表達式時可以選擇其他順序。

這里沒有對與錯,GCC評估從右到左,從左到右的叮當聲

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM