Re: Initialising std::vector from a sequence using std::istringstream
"Paul" <vhr@newsgroups.nospam> wrote in message
news:2A4B1ECD-9C2B-4381-8941-311DF20200B7@microsoft.com
std::vector<std::string> v(std::istream_iterator<std::string>(lst),
std::istream_iterator<std::string>());
std::cout << "abc = " << *v.begin() << std::endl;
the compiler complains like this:
error C2228: left of '.begin' must have class/struct/union
Scott Meyers calls this C++'s "most vexing parse" (look it up on
Google). You have _not_ declared a variable named v of type
vector<string>. You have declared a function named v that returns
vector<string> and takes two parameters: one named lst of type
istream_iterator<string>, and an unnamed one of type "function with no
parameters returning istream_iterator<string>".
One workaround is to add an extra pair of parentheses:
vector<string> v(
(istream_iterator<string>(lst)),
istream_iterator<string>());
With these additional parens, the statement can no longer be parsed as a
function declaration and thus becomes a variable declaration with
initializer.
--
With best wishes,
Igor Tandetnik
With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925