C++17 std::optional error: expected primary-expression before 'auto'

后端 未结 1 1339
清酒与你
清酒与你 2020-12-12 06:52

I was experimenting with C++17 feature std::optional

The optional return type is std::optional>. I call the sum_p

相关标签:
1条回答
  • 2020-12-12 07:12
    if(auto Pair = sum_pair(vec, sum) )
        std::cout << "Resulting indexes are: " << Pair->first << " " << Pair->second << std::endl;
    else
        std::cout << "Nothing found!\n";
    

    this is valid C++. You are allowed to put a declaration in the opening condition of an if clause.

    (auto Pair = sum_pair(vec, sum) )?
        std::cout << "Resulting indexes are: " << Pair->first << " " << Pair->second << std::endl
    :
        std::cout << "Nothing found!\n";
    

    this is not valid C++. Declarations are not expressions. There are places where expressions are allowed, but declararions are not. The left hand side of ?, the trinary operator, is one of them.

    0 讨论(0)
提交回复
热议问题