Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

c++ - Vector constructor with two parameters is parsed as a function declaration

Consider this example:

#include <iostream>
#include <string>
#include <vector>
#include <iterator>

int main()
{
    std::string sen = "abc def ghi jkl";
    std::istringstream iss(sen);

    std::vector<std::string>    // declaration in question
    vec(std::istream_iterator<std::string>(iss),
        std::istream_iterator<std::string>());

    std::copy(vec.begin(), vec.end(),
              std::ostream_iterator<std::string>(std::cout, "
"));
}

The compiler throws an error at the call to std::copy

request for member 'begin' in 'vec', which is of non-class type...

I can get around the error like this:

std::istream_iterator<std::string> it_begin(iss);
std::istream_iterator<std::string> it_end;
std::vector<std::string> vec(it_begin, it_end);

or by putting parentheses around each parameter, like this:

std::vector<std::string>
vec((std::istream_iterator<std::string>(iss)),
    (std::istream_iterator<std::string>()));

or even with the new uniform initialization in C++11:

std::vector<std::string> vec { /*begin*/, /*end*/ };

Why is compiler parsing the declaration in the example as a function declaration? I know about most vexing parse, but I thought that only happens with empty parameter lists. I also wonder why the second workaround works.

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

It's still the most vexing parse.

std::vector<std::string>                     // return type
vec(                                         // function name
    std::istream_iterator<std::string>(iss), // param 1: an iterator called (iss), or just iss
    std::istream_iterator<std::string>()     // param 2: unnamed function 
);                                           //          returning iterator

geordi says:

<tomalak> << ETYPE_DESC(vec); std::vector<std::string> vec(std::istream_iterator<std::string>(iss), std::istream_iterator<std::string>());
<geordi> lvalue function taking a istream_iterator<string, char, char_traits<char>, long> , a pointer to a nullary function returning a istream_iterator<string, char, char_traits<char>, long> , and returning a vector of strings

The crux of it, really, is that your parameter names can have parentheses around them (i.e. iss(iss)) without altering the semantics of the declaration. Sometimes.

Use another set of parentheses that also surround the type, as you showed, to force that first parameter (and, consequently, the second) to be parsed as an expression rather than a declaration.


If it helps, also consider:

void foo(int (x)) {
   cout << x;
}

int main() {
   foo(42);
}

Output is 42.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...