Passing temporary istringstream object to istream_iterator<string>

白昼怎懂夜的黑 提交于 2019-12-20 02:26:13

问题


I have a question about the following code that tokenizes a string (separates the tokens by space).

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

using namespace std;

int main()
{
    string s="And I feel fine ...";
    istringstream ss(s);
    vector<string> tokens{istream_iterator<string>(ss),{}};
    for(auto& elem: tokens)
        cout << elem << endl;
}

This works perfectly fine. On the other hand, if I try to pass a temporary istringstream object to istream_iterator (3-rd line inside main), such as

vector<string> tokens{istream_iterator<string>(istringstream(s)),{}};

I am getting a compile-time error error: no matching conversion for functional-style cast from 'istringstream' (aka 'basic_istringstream<char>') to 'istream_iterator<string>'

I believe it is because I cannot bound a temporary rvalue to a non-const lvalue reference, and the constructor of istream_iterator takes a reference as a parameter. Is there any way of constructing an istream_iterator<string> from a temporary? I cannot use std::ref on a temp object either...

Thanks!


回答1:


Use the std::skipw trick:

std::vector<std::string> tokens{
    std::istream_iterator<std::string>(std::istringstream(s) >> std::skipws), {}
};

This works because the extractor returns an lvalue-reference to the stream, and setting std::skipw won't have any effect because it is enabled by default on all streams. To use this in the general case, you would want to have a function that takes a universal reference, then returns a reference to that object:

template<typename T>
T& lvalue(T&& x)
{
    return x;
}

// ...
std::istream_iterator<std::string>(lvalue(std::istringstream(s)));

Make sure the reference isn't being used past the full expression, or else you'll have a dangling reference as the temporary will have already been destructed.



来源:https://stackoverflow.com/questions/23714445/passing-temporary-istringstream-object-to-istream-iteratorstring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!