@TheToaster
I don't want either you or
@mbozzi to take this personal but you have both missed the point not just of the OP but also misread my post on how to make it work and how it should be done.
Your code doesn't seem to work with raw C-style arrays: |
Not true it works just fine with C-style arrays
mbozzi's code works just fine with both constructors for me |
Well my code also works just fine, if you look at comments I've put into code sample in my previous code post you'll see this:
1 2
|
// Uncomment bellow line to accept ex. std::vector iterator
// using iterator_category = std::random_access_iterator_tag
|
If you uncomment this line it will work.
1 2 3 4 5 6 7 8
|
class iterator
{
// Define your iterator traits however you like here
public:
using iterator_category = std::random_access_iterator_tag /* THIS */
// ...
};
|
1 2 3 4 5 6
|
int main()
{
// works just fine!
int arr[] {1,2,3,4,5};
List<int> arr_list(arr, arr+5);
}
|
Now, the major issue you people missed is that OP want's his iterator to be
input iterator not
random access iterator which is FYI not the same, and to construct from C style array requires random access iterator.
in mbozzi code version he defines constructor like this: (added impl. for testing)
1 2 3 4 5
|
template<typename InputIterator, typename = decltype(*std::declval<InputIterator>())>
List(InputIterator first, InputIterator last)
{
std::cout << "List(InputIterator, InputIterator)" << std::endl;
}
|
but the OP want's his iterator to be:
using iterator_category = std::input_iterator_tag;
Do you see the problem?
The problem is that the design of the OP's iterator is now broken
1 2 3
|
// THIS WILL WORK, BUT IT SHOULD NOT!
std::vector<int> vec_lis{ 1,2,3,4,5 };
List<int> vec_lis_test(vec_lis.begin(), vec_lis.end());
|
the above works, but OP defined his
List
class iterator as input iterator but
std::vector
iterator is random access iterator, so the promise has been broken, and the design is bad, as simple as that.
Ofc. if the OP wants to accept random access iterator he just defines is like this:
using iterator_category = std::input_iterator_tag;
and everthing will work just fine,
in which case even the mbozzi version is valid!