views:

217

answers:

3

I was wondering about the last constructor for std::string mentioned here. It says:

template<class InputIterator> string (InputIterator begin, InputIterator end);

If InputIterator is an integral type, behaves as the sixth constructor version (the one right above this) by typecasting begin and end to call it:

string(static_cast<size_t>(begin),static_cast<char>(end));

In any other case, the parameters are taken as iterators, and the content is initialized with the values of the elements that go from the element referred by iterator begin to the element right before the one referred by iterator end.

So what does that mean if InputIterator is a char * ?

EDIT: Ok, my bad. I just realized that it says integral type, not primitive type in the documentation, so the question does not apply to that example. But still, are pointers primitives?

A: 
char * str = "Some string";
std::string s(str, str+6); // s = "Some s";
Alexey Malistov
A: 

C++ pointers implement quite well the concept of InputIterator (after all, STL iterators are a generalization of C++ pointers). So the two arguments are considered as pointers to an array of char designating the first and the "one past end" elements needed to initialize the string.

AProgrammer
InputIterator can be an std::list iterator, which does not constitute a pointer to an array of anything.
iconiK
@iconiK: I didn't wrote InputInterator were all pointers inside an array, I wrote that pointers inside an array fullfilled all requirements of InputIterator (in fact Bidirectional Iterators) and that it wasn't by chance, that Bidirectional Iterators were designed to be an abstraction of the behavior of pointers inside an array.
AProgrammer
+9  A: 

C++ doesn't have a concept of "primitive" types; integers are fundamental types and pointers are compound types.

In this case, char* can't be converted into either size_t or char, so it will be taken as the InputIterator template parameter.

Mike Seymour
Plus, both integers and pointers are "scalar types".
FredOverflow