vector size() argument ?


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
1
2
3
4
5
6
7
8
9
10
11
12
namespace {
  template <typename Range>
  void fetch_impl(Range&& r) { 
    for (const auto & s : std::forward<Range>(r))
      std::cout << s << "\n";
  }
}

void ftnode_client::fetch(std::size_t start, std::size_t end)
{ fetch_impl(make_range(v_result, start, end)); }
void ftnode_client::fetch(std::size_t start=0) 
{ fetch_impl(make_range(v_result, start, v_result.size())); }


I'm seeing this error?
 
error: expected unqualified-id before ‘namespace

for the code you suggested, which I've placed into the .cpp file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include "ftnode_client.h"

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);
}

namespace {
  template <typename Range>
  void fetch_impl(Range&& r) { 
    for (const auto & s : std::forward<Range>(r))
      std::cout << s << "\n";
  }
};

Tried moving them to the .h file, but compile generated even more errors.
Any ideas ?

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

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..
Topic archived. No new replies allowed.