auto

Using auto as a template parameter

試著忘記壹切 提交于 2019-12-23 06:01:45
问题 I'm attempting to compile the following using GCC 4.7.1 with the -std=c++11 flag set: std::map<std::string, auto> myMap; I'm attempting to create an object to contain a large amount of Json data of various types (int string, bool) as well as sub-structures (list, map) so I can't declare the type of the field value at compile time, so I thought I'd use the auto keyword for it. However, when I try to compile it, I get the following error: invalid use of ‘auto’ error: template argument 2 is

auto&& variable's are not rvalue reference

混江龙づ霸主 提交于 2019-12-22 09:54:27
问题 Why auto&& is not rvalue reference? Widget&& var1 = Widget(); // rvalue reference auto&& var2 = var1; //var2 not rvalue reference below are rvalue reference example void f(Widget&& param); // rvalue reference Widget&& var1 = Widget(); // rvalue reference Why var2 is not rvalue reference but f and var2 are rvalue references? 回答1: Once the type of the initializer has been determined, the compiler determines the type that will replace the keyword auto using the rules for template argument

auto parameter type in functions

你说的曾经没有我的故事 提交于 2019-12-22 09:29:22
问题 I would like to know if the standard committee considered expanding the C++14 auto keyword to deduce function template parameter type, as it exists today in generic lambdas. (as can be seen nicely depicted in this answer) Because it works in lambda functions, it should also work in any function. Of course it would be totally redundant with the classic syntax: template< typename T > void f(T param); But being able to write this, for the same result: void f(auto param); I think would allow for

C++11: Private member security [duplicate]

此生再无相见时 提交于 2019-12-22 08:18:20
问题 This question already has answers here : Why can I use auto on a private type? (4 answers) Closed 6 years ago . Let's consider the next code: #include <iostream> #include "mydemangled.hpp" using namespace std; struct A { private: struct B { int get() const { return 5; } }; public: B get() const { return B(); } }; int main() { A a; A::B b = a.get(); cout << demangled(b) << endl; cout << b.get() << endl; } And the compiler (gcc 4.7.2) yells saying that A::B is private. All right. So, I change

What was `auto` used for before?

百般思念 提交于 2019-12-22 04:39:22
问题 I know that before C++11 the auto keyword had a completely different meaning; it was a storage type specifier indicating an object that has automatic storage type (ie, placed on the stack). That's how the theory goes... How would you actually use this keyword (syntax), and why? Also, I haven't seen this keyword in actual code pre-C++11; when was it useful (what time period)? 回答1: It was used to declare a local variable with automatic storage duration (i.e., "on the stack"). At least since C90

What are the type deduction rules for auto*?

北慕城南 提交于 2019-12-21 21:35:48
问题 What are the type deduction rules for auto* ? Consider the following: int x = 64; int* px = &x; auto* v1 = &x; // auto => ??? ok v1 is int* ... auto* v2 = px; // auto => ??? is v2 int* ? auto* v3 = &px; // auto => ??? is v3 int** ? Just to clarify my question if we split the type deduction into two steps: Deducing the type of " auto " itself without ( * ) ... then Deducing the type of the object ( v1 , v2 and v3 ) after adding the ( * ) So my two questions are: What will auto be deduced to

Can the use of C++11's 'auto' deteriorate performance or even break the code?

試著忘記壹切 提交于 2019-12-21 19:45:26
问题 This question is the opposite of an existing question "Can the use of C++11's 'auto' improve performance?" One of the answers to that question indicated that usage of auto can have not only positive but also negative effects. I believe we need a separate question, with answers focusing on that side of auto . 回答1: With auto there is no conversion at the variable declaration+initialization line. But if such conversion must happen anyway, it better happen once during initialization than multiple

Why type-deduction for arrays prioritizes pointer to first over reference to array?

。_饼干妹妹 提交于 2019-12-21 11:45:08
问题 int v[1]; auto p1 = v; auto &p2 = v; auto *p3 = v; p1 is of type int * (same for p3 ). Particularly at this trivial sample I find p2 ( int (&)[1] ) more useful since it inherents array semantics, e.g. I can apply sizeof on p2 to give the same as sizeof on v . Is there a standard quotation regarding that? Why defaulting to references is a bad idea? (for this arrays case I mean, almost no c++ programmer cares about them these days anyway...) 回答1: auto deduces a non-reference type. auto& deduces

Advantage of using trailing return type in C++11 functions

我怕爱的太早我们不能终老 提交于 2019-12-21 10:21:12
问题 What is the advantage of specifying a trailing return type in C++11, as opposed to a normal return type? Look at foo1 vs foo2 here: int foo1() { return 1; } auto foo2() -> int { return 1; } int main() { foo1(); foo2(); } 回答1: In this example, they mean the exact same thing. However, there are a few advantages to using the trailing return type form consistently (Phil Nash calls these "East End Functions", since the return type is on the east end). Using parameters. Obviously when using

Advantage of using trailing return type in C++11 functions

梦想的初衷 提交于 2019-12-21 10:21:03
问题 What is the advantage of specifying a trailing return type in C++11, as opposed to a normal return type? Look at foo1 vs foo2 here: int foo1() { return 1; } auto foo2() -> int { return 1; } int main() { foo1(); foo2(); } 回答1: In this example, they mean the exact same thing. However, there are a few advantages to using the trailing return type form consistently (Phil Nash calls these "East End Functions", since the return type is on the east end). Using parameters. Obviously when using