auto

Unusual behavior with auto while traversing a dynamic vector

北城余情 提交于 2019-12-01 05:24:21
I am traversing a vector with auto ( code attached ). While traversing, I am also appending some elements at the back. I was not expecting the output that I got. #include <iostream> #include <vector> using namespace std; vector <int> dynamic_vector; void access( ) { for ( auto i : dynamic_vector ) { if ( i == 3 ) { dynamic_vector.push_back( 4 ); dynamic_vector.push_back( 5 ); } cout << i << endl; } } int main() { dynamic_vector.push_back( 1 ); dynamic_vector.push_back( 2 ); dynamic_vector.push_back( 3 ); access( ); return 0; } Output: 1 2 3 I was expecting all numbers from 1 to 5 will get

auto it = vector.begin() resulting type is not convertible to const_iterator

橙三吉。 提交于 2019-12-01 04:58:30
问题 Containers are required to provide an iterator type which is implicitly convertible to a const_iterator . Given this, I am trying to use auto to initialize an object via vector::begin() , and use that resulting object in std::distance where the RHS is a const_iterator . This isn't working. Here is a complete example: #include <cstdlib> #include <vector> #include <iterator> #include <iostream> typedef std::vector <char> Packet; typedef std::vector <Packet> Packets; template <typename Iter>

Why is there a special type deduction rule for auto and braced initializers in C++11/C++14?

我与影子孤独终老i 提交于 2019-12-01 04:47:39
In his CppCon 2014 talke "Type Deduction and Why You Care" , Scott Meyers raises the question why there is a special rule about auto and braced initializers in the C++11/C++14 standard (his question starts at 36m05s ). The semantic of auto in combination with a braced-init-list is defined in §7.1.6.4/6. I thought about it and could not come up with a use-case either. The closest thing that I have seen so far is one example where Bjarne Stroustrup used it. In his Cpp 2014 talk "Make Simple Tasks Simple!" , he once uses auto to capture initializers (but only as a workaround). Here is the code

C & C++: What is the difference between pointer-to and address-of array?

烂漫一生 提交于 2019-12-01 04:44:40
C++11 code: int a[3]; auto b = a; // b is of type int* auto c = &a; // c is of type int(*)[1] C code: int a[3]; int *b = a; int (*c)[3] = &a; The values of b and c are the same. What is the difference between b and c ? Why are they not the same type? UPDATE: I changed the array size from 1 to 3. Joe Z The sizeof operator should behave differently, for one, especially if you change the declaration of a to a different number of integers, such as int a[7] : int main() { int a[7]; auto b = a; auto c = &a; std::cout << sizeof(*b) << std::endl; // outputs sizeof(int) std::cout << sizeof(*c) << std:

How would auto&& extend the life-time of the temporary object?

☆樱花仙子☆ 提交于 2019-12-01 03:23:54
The code below illustrated my concern: #include <iostream> struct O { ~O() { std::cout << "~O()\n"; } }; struct wrapper { O const& val; ~wrapper() { std::cout << "~wrapper()\n"; } }; struct wrapperEx // with explicit ctor { O const& val; explicit wrapperEx(O const& val) : val(val) {} ~wrapperEx() { std::cout << "~wrapperEx()\n"; } }; template<class T> T&& f(T&& t) { return std::forward<T>(t); } int main() { std::cout << "case 1-----------\n"; { auto&& a = wrapper{O()}; std::cout << "end-scope\n"; } std::cout << "case 2-----------\n"; { auto a = wrapper{O()}; std::cout << "end-scope\n"; } std:

Is there a way to disable auto declaration for non regular types?

杀马特。学长 韩版系。学妹 提交于 2019-12-01 03:23:04
UPDATE: There is a proposal to change the meaning of auto in certain situations. Implicit Evaluation of “auto” Variables and Arguments by Joel Falcou and others. The implicit evaluation shall: Enable class implementers to indicate that objects of this class are evaluated in an auto statement; Enable them to determine the type of the evaluated object; ... C++11's auto keyword is great. However in my opinion if a type is Not Regular (see for example, What is a "Regular Type" in the context of move semantics? ) the usage of auto becomes tricky. Is there a way to disable the auto declaration for

C & C++: What is the difference between pointer-to and address-of array?

狂风中的少年 提交于 2019-12-01 03:06:00
问题 C++11 code: int a[3]; auto b = a; // b is of type int* auto c = &a; // c is of type int(*)[1] C code: int a[3]; int *b = a; int (*c)[3] = &a; The values of b and c are the same. What is the difference between b and c ? Why are they not the same type? UPDATE: I changed the array size from 1 to 3. 回答1: The sizeof operator should behave differently, for one, especially if you change the declaration of a to a different number of integers, such as int a[7] : int main() { int a[7]; auto b = a; auto

Why does C++ not allow multiple types in one auto statement?

我们两清 提交于 2019-12-01 02:49:58
The 2011 C++ standard introduced the new keyword auto , which can be used for defining variables instead of a type, i.e. auto p=make_pair(1,2.5); // pair<int,double> auto i=std::begin(c), end=std::end(c); // decltype(std::begin(c)) In the second line, i and end are of the same type, referred to as auto . The standard does not allow auto i=std::begin(container), e=std::end(container), x=*i; when x would be of different type. My question : why does the standard not allow this last line? It could be allowed by interpreting auto not as representing some to-be-decuded type, but as indicating that

Why is there a special type deduction rule for auto and braced initializers in C++11/C++14?

醉酒当歌 提交于 2019-12-01 02:42:31
问题 In his CppCon 2014 talke "Type Deduction and Why You Care", Scott Meyers raises the question why there is a special rule about auto and braced initializers in the C++11/C++14 standard (his question starts at 36m05s). The semantic of auto in combination with a braced-init-list is defined in §7.1.6.4/6. I thought about it and could not come up with a use-case either. The closest thing that I have seen so far is one example where Bjarne Stroustrup used it. In his Cpp 2014 talk "Make Simple Tasks

Unusual behavior with auto while traversing a dynamic vector

好久不见. 提交于 2019-12-01 02:37:22
问题 I am traversing a vector with auto ( code attached ). While traversing, I am also appending some elements at the back. I was not expecting the output that I got. #include <iostream> #include <vector> using namespace std; vector <int> dynamic_vector; void access( ) { for ( auto i : dynamic_vector ) { if ( i == 3 ) { dynamic_vector.push_back( 4 ); dynamic_vector.push_back( 5 ); } cout << i << endl; } } int main() { dynamic_vector.push_back( 1 ); dynamic_vector.push_back( 2 ); dynamic_vector