Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
659 views
in Technique[技术] by (71.8m points)

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

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

The optional return type is std::optional<std::pair<int, int>>. I call the sum_pair function in print_answer function and wanted a optional print.

In print_answer function I wanted to check whether the required pair holds something to show. like in the example given in: optional-returning factory functions are usable as conditions of while and if

Following is the code: here is it live with error

#include <iostream>
#include <vector>
#include <unordered_map>
#include <optional>

typedef std::optional<std::pair<int, int>> returnType;

// following algorithum works fine: just to show, 
// how I have used the std::optional
returnType sum_pair(const std::vector<int>& vec, const int sum)
{
    std::unordered_map<int, int> compIndexMap;

    int index = 0;
    for(const int& ele: vec)
    {
        if(auto check = compIndexMap.find(sum - ele); check != compIndexMap.cend())
            return returnType{std::make_pair(check->second, index)};

        compIndexMap.emplace(sum - ele, index);
        ++index;
    }
    return std::nullopt;
}

// problem is here:
void print_answer(const std::vector<int>& vec, const int sum)
{
    // if I uncomment the if-else, everything works
    /*if*/(auto Pair = sum_pair(vec, sum) )?  
        std::cout << "Resulting indexes are: " << Pair->first << " " << Pair->second << std::endl: //;
    //else
        std::cout << "Nothing found!
";
}
int main()
{
    std::vector<int> vec0{ 1,3,2,8 };
    const int sum = 8;
    print_answer(vec0, sum);
    return 0;
}

When I use the if-else statement in the following format

(condion) ? print something: print something else;

I get the following two errors. (used GCC 7.1)

||=== Build: Debug in MyTestProgram (compiler: GNU GCC Compiler) ===|
|25|error: expected primary-expression before 'auto'|
|25|error: expected ')' before 'auto'|

Can somebody explain, why I need to use if-else, but not with "operator ?" ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
if(auto Pair = sum_pair(vec, sum) )
    std::cout << "Resulting indexes are: " << Pair->first << " " << Pair->second << std::endl;
else
    std::cout << "Nothing found!
";

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!
";

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...