auto

C++11 Change `auto` Lambda to a different Lambda?

拟墨画扇 提交于 2019-11-26 17:22:46
问题 Say I have the following variable containing a lambda: auto a = [] { return true; }; And I want a to return false later on. Could I do something along the lines of this? a = [] { return false; }; This syntax gives me the following errors: binary '=' : no operator found which takes a right-hand operand of type 'main::<lambda_a7185966f92d197a64e4878ceff8af4a>' (or there is no acceptable conversion) IntelliSense: no operator "=" matches these operands operand types are: lambda []bool ()->bool =

C++ auto vs auto&

人盡茶涼 提交于 2019-11-26 16:28:24
问题 if I have a function: Foo& Bar() { return /// do something to create a non-temp Foo here and return a reference to it } why is this: auto x = Bar(); /// probably calls copy ctor - haven't checked not the same as this? auto &x = Bar(); /// actually get a reference here (Actually, I'd expect the second version to get a reference to a reference, which makes little sense.) If I explicitly specified the type of x as a value or a reference, I'll get what I expect (of course). I would expect, though

C++11 - declaring non-static data members as &#39;auto&#39;

人走茶凉 提交于 2019-11-26 15:51:52
问题 Does C++11 allow declaring non-static data members as 'auto' if they are initialized in the declaration? For example: struct S { auto x = 5; // in place of 'int x = 5;', which is definitely allowed }; GCC 4.7 rejects the above code, while it accepts int x = 5; . Assuming this is not a compiler bug but rather the standard really doesn't allow it, why not? It would be just as useful as declaring local variables auto . 回答1: The rule for prohibiting non-static members is in 7.1.6.4 clause 4: The

Is auto as a parameter in a regular function a GCC 4.9 extension?

社会主义新天地 提交于 2019-11-26 15:31:57
gcc 4.9 allows the following code, but gcc 4.8 and clang 3.5.0 reject it. void foo(auto c) { std::cout << c.c_str(); } I get warning: ISO C++ forbids use of 'auto' in parameter declaration [-Wpedantic] in 4.9 but in 4.8 and clang I get error: parameter declared 'auto' . Yes, this is an extension. It's likely to be added to C++17 as part of the 'concepts' proposal, I believe. This is Concepts Lite speak for template<class T> void foo(T c) { std::cout << c.c_str(); } The auto just replaces the more verbose template<class T> . Similarly, you can write void foo(Sortable c) as a shorthand for

Why does auto x{3} deduce an initializer_list?

梦想与她 提交于 2019-11-26 15:25:52
I love auto in C++11. It's wonderful. But it has one inconsistency that really gets on my nerves, because I trip over it all the time: int i = 3; // i is an int with value 3 int i = int{3}; // i is an int with value 3 int i(3); // i is an int with value 3 (possibly narrowing, not in this case) int i{3}; // i is an int with value 3 auto i = 3; // i is an int with value 3 auto i = int{3}; // i is an int with value 3 auto i(3); // i is an int with value 3 auto i{3}; // wtf, i is a std::initializer_list<int>?! This strange behaviour is confusing for newcomers, and annoying for experienced users --

Advantages of auto in template parameters in C++17

ε祈祈猫儿з 提交于 2019-11-26 15:21:36
What are the advantages of auto in template parameters that will (possibly) be introduced with C++17? Is it just a natural extension of auto when I want to instantiate template code? auto v1 = constant<5>; // v1 == 5, decltype(v1) is int auto v2 = constant<true>; // v2 == true, decltype(v2) is bool auto v3 = constant<'a'>; // v3 == 'a', decltype(v3) is char What else do I gain from this language feature? The template <auto> feature ( P0127R1 ) was accepted into C++ in the ISO C++ 2016 meeting in Oulu, Finland. An auto keyword in a template parameter can be used to indicate a non-type parameter

The new keyword “auto”; When should it be used to declare a variable type? [duplicate]

爱⌒轻易说出口 提交于 2019-11-26 15:07:19
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: How much is too much with C++0x auto keyword Have we (as a community) had enough experience to determine when and/or whether auto is being abused? What I am really looking for is a best practices guide on when to use auto when it should be avoided Simple rules of thumb that can quickly be followed in 80% of cases. As a context this question is sparked by my response here 回答1: I think when the type is very well

C++11 auto和decltype

痞子三分冷 提交于 2019-11-26 14:26:28
1.C++11 auto 在C++98中,auto的用法为变量为自动变量,拥有自动的生命周期。但显然,变量默认都是有自动生命周期的。 在C++11中,auto的用法为:推断变量的类型。 举例如下: int i = 1; auto j = i; 在该例子中,auto让变量的类型不直接获得而是推断得知。 从C++语言角度来讲,C++是强类型语言,必须确定变量的类型。而python等弱类型语言,不直接确定变量类型,而是确定让变量在运行时确定类型。C++ 11显然是借鉴了这个特性。但从另一方面讲,auto变量是不是违反了C++强类型语言的特点,值得商榷。 auto变量,可以让代码看起来简化,但运行效率,显然会变低。 2.C++11 decltype decltype,用于检查变量的类型,从而让变量类型确定。 举例如下: struct A { double x; }; const A* a; decltype(a->x) y; // y 的类型是 double(其声明类型) decltype((a->x)) z = y; // z 的类型是 const double&(左值表达式) 3.样例 # include <vector> # include <iostream> template<class T1,class T2> void add(T1 v1,T2 v2) { decltype

Why doesn&#39;t the C++11 &#39;auto&#39; keyword work for static members?

人走茶凉 提交于 2019-11-26 14:17:01
问题 class Foo { public: static const char *constant_string; }; auto Foo::constant_string = "foo"; int main(void) { }; Compiled with: gcc (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3 like this: gcc -std=c++0x ./foo.cc ./foo.cc:6:11: error: conflicting declaration ‘auto Foo::constant_string’ ./foo.cc:3:22: error: ‘Foo::constant_string’ has a previous declaration as ‘const char* Foo::constant_string’ ./foo.cc:6:11: error: declaration of ‘const char* Foo::constant_string’ outside of class is not definition [

What are some uses of decltype(auto)?

对着背影说爱祢 提交于 2019-11-26 14:03:14
In c++14 the decltype(auto) idiom is introduced. Typically its use is to allow auto declarations to use the decltype rules on the given expression . Searching for examples of "good" usage of the idiom I can only think of things like the following (by Scott Meyers ), namely for a function's return type deduction : template<typename ContainerType, typename IndexType> // C++14 decltype(auto) grab(ContainerType&& container, IndexType&& index) { authenticateUser(); return std::forward<ContainerType>(container)[std::forward<IndexType>(index)]; } Are there any other examples where this new language