Unified Iterator Initialization

I am very new to C ++ 11 and there is a problem with iterators and uniform initialization that I don't understand.

Consider the following example, which does not compile:

#include <iostream>
#include <vector>

int main() {

    std::vector<int> t{1, 2, 3, 4, 5};
    auto iter{t.begin()};                   

    for (; iter != t.end(); ++iter) {
        std::cout << *iter;
    }

    return 0;
}

In line 6, the vector is initialized using uniform initialization. On line 7, I'm trying to do the same with an iterator. This does not work. Changing line 7 to auto iter = t.begin()is OK. I know that I can simply use a range-based range for this, but the question arises: why does uniform initialization not work with iterators, but is it good with basic types, for example int i{0};?

+4
source share
2

auto, . , iter std::initializer_list<vector<int>::iterator>, vector<int>::iterator, .

auto iter = t.begin() - .

+4

++ 11 auto , std::initializer_list. :

auto iter(t.begin());
auto iter = t.begin();

:

§7.1.6.4/7 auto specifier [dcl.spec.auto]

T - . -, . , - -init-list (8.5.4), . P T, auto U, , , std:: initializer_- . U, (14.8.2.1), P - , - . , . , , U P.

( ).

. .
+3

Source: https://habr.com/ru/post/1544667/


All Articles