modyfikator auto

0
int k =0;
auto a { k } ;
auto b = {k};

Co oznacza ten kod?

1

W zależności od wersji C++:

W C++03 ten kod jest błędny i się nie skompiluje.
W C++11 ten kod oznacza tyle, że kompilator ma się sam domyślić typu zmiennych. Co do nawiasów klamrowych to jest to nowy sposób na odpalanie konstruktorów (w tym przypadku pseudokonstruktorów). Czyli kompilator to zbuduje jak:

int k = 0;
int a(k);
10

auto pozwala dedukować typy i zapis

auto c = k;

jest równoznaczny z int c = k;


Definicje `a` i `b` są jednoznaczne (ponieważ `=` jest opcjonalne), ale `{k}` jest interpretowane jako `std::initializer_list<int>(k)`, czyli
```cpp
std::initializer_list<int> a = {k};
std::initializer_list<int> b = {k};

Dowód: http://melpon.org/wandbox/permlink/4seCRctTddFeJpXt

inicjalizacja przez {} to nowy, lepszy, sposób inicjalizacji wprowadzony w C++11, nazwany "uniform initialization". Ma jednak pewną przypadłość - wszystko co może zostać uznane za listę incjalizacyjną zostanie za nią uznane (co potrafi znacząco różnić się od inicjalizacji za pomocą ()). Jeśli się tak nie da, obiekt zostanie zainicjalizowany z przekazanymi wartościami, tak jak za pomocą ().
Przykłady:

vector<int> a(3,5); // { 5, 5, 5}
vector<int> b{3,5}; // { 3, 5}

Lub tak jak wyżej:

int k = 42;
auto a{k}; // initializer_list<int> a = {k};
auto b(k); // int b = 42;
1
kq napisał(a)
vector<int> a(3,5); // { 5, 5, 5}
vector<int> b{3,5}; // { 3, 5}

Dlatego odradzam pisanie vector<int> b{3,5}, jeśli już to

vector<int> b = {3, 5};

Być może błędem było wpisanie do standardu, że znak = ma być opcjonalny.

0
   int v =2;     
    int& ref = v;
    auto e = ref; 

Nie rozumiem jednej rzeczy. Mówi się, że auto dedukuje typ mojej zmiennej e patrząc się na to, co jest po prawej stronie. Co mamy po prawej? Jaki typ? A no referencję do zmiennejr typu int. W takim razie e również powinno być referencją do v. W książce czytam, że:
<ort> auto e = ref; <=> auto e = v</ort>
Czyli, że e NIE jest referencją, a JEST zmienną INT.
Proszę o rozjaśnienie :)
pozdro!

2

Tak jak napisał @Satirev w komentarzu - auto (za wyjątkiem list inicjalizacyjnych) działa na dokładnie tej samej zasadzie co szablony w C++. Jest to dokładnie opisane w §7.1.6.4 [dcl.spec.auto]/7 w standardzie (bazując na N3797):

Word of God napisał(a)

When a variable declared using a placeholder type is initialized [...] the deduced return type or variable type is determined from the type of its initializer. Let T be the declared type of the variable [...] If the placeholder is the auto type-specifier , the deduced type is determined using the rules for template argument deduction. [...] obtain P from T by replacing the occurrences of auto with either a new invented type template parameter U or, if the initializer is a braced-init-list , with std::initializer_list<U>. Deduce a value for U using the rules of template argument deduction from a function call, where P is a function template parameter type and the initializer is the corresponding argument. If the deduction fails, the declaration is ill-formed. Otherwise, the type deduced for the variable or return type is obtained by substituting the deduced U into P

Inaczej mówiąc, tak samo jak dla

template<typename T>
void f(T)
{
}

f(ref) wywoła f<int>(ref), a nie f<int&>(ref), tak samo auto e = ref będzie typu int, a nie int&.

Czyli upraszczając: najbardziej zewnętrzne const/volatile/ref znikają.

@Azarien: bez ominięcia = nie szło by użyć uniform initialization w listach inicjalizacyjnych konstruktorów. A jeśli tam by zrobić wyjątek to znów jest problem jeśli vector<int> jest elementem klasy.

3

http://www.cprogramming.com/c++11/c++11-auto-decltype-return-value-after-function.html

The short answer is in C++11, auto defaults to being by-value for references, so in the above code bar is an int. However, you can add the & as a modifier to force it to be a reference.

Czyli auto& e = ref już sprawi, że e będzie int&.

1 użytkowników online, w tym zalogowanych: 0, gości: 1