Sep 14, 2017 at 10:51pm UTC
I tried doing this earlier:
1 2 3 4
void ftnode_client::fetch(std::size_t start=0, std::size_t end=v_result.size() ) {
for (const auto & s : make_range(v_result, start, end) )
std::cout << s << "\n" ;
}
and of course, I got compile errors, since you cannot use size() in this way. I then came across this post:
https://stackoverflow.com/questions/9227333/c-templates-vector-size-used-in-default-parameter-definitoin
and I was wondering if somebody could help me figure out if I can apply the overloaded type in the post to my function, which is:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
template <class Iter>
class range {
Iter b; Iter e;
public :
range(Iter b, Iter e) : b(b), e(e) {}
Iter begin() { return b; }
Iter end() { return e; }
};
template <class Container>
range<typename Container::iterator>
make_range(Container& c, size_t b, size_t e) {
return range<typename Container::iterator> (c.begin()+b, c.begin()+e);
}
std::vector<std::string> v_result
void ftnode_client::fetch(std::size_t start=0, std::size_t end=v_result.size() ) {
for (const auto & s : make_range(v_result, start, end) )
std::cout << s << "\n" ;
}
How is it possible to modify the fetch function to an overloaded type that can then use size() ?
Last edited on Sep 14, 2017 at 10:52pm UTC
Sep 15, 2017 at 12:10am UTC
When I compile the above (without the header, of course) the only error I get is from the spurious semicolon on line 25. (A namespace is not a statement, so it doesn't need to end with a semicolon.)
Is the end of ftnode_client.h missing a semicolon? Does it contain mismatched braces?
Last edited on Sep 15, 2017 at 12:14am UTC
Sep 15, 2017 at 12:42am UTC
got it working
i understand the theory of templates, but struggle with creating scenarios such as what you have posted.
thanks, I guess it'll come with time..