auto

`auto` return type in context of class members

我与影子孤独终老i 提交于 2019-11-27 06:10:52
问题 How can automatic type deduction be used for class members? For example, the following code struct A { auto foo(); // foo is defined in another file }; int main() { A a; a.foo(); } where foo has the return type auto results in the following error: error: function 'foo' with deduced return type cannot be used before it is defined a.foo(); ^ The error is comprehensible since the compile cannot know what foo 's return type is without knowing its definition. My question is, if there is any

Can the use of C++11's 'auto' improve performance?

試著忘記壹切 提交于 2019-11-27 05:48:39
I can see why the auto type in C++11 improves correctness and maintainability. I've read that it can also improve performance ( Almost Always Auto by Herb Sutter), but I miss a good explanation. How can auto improve performance? Can anyone give an example? auto can aid performance by avoiding silent implicit conversions . An example I find compelling is the following. std::map<Key, Val> m; // ... for (std::pair<Key, Val> const& item : m) { // do stuff } See the bug? Here we are, thinking we're elegantly taking every item in the map by const reference and using the new range-for expression to

Why do lambda functions drop deduced return type reference by default?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-27 05:12:31
In C++14, why do lambda functions with a deduced return type drop references from the return type by default? IIUC, since C++14 lambda functions with a deduced return type (without an explicit trailing return type) have a return type of auto , which drops references (among other things). Why was this decision made? It seems to me like a gotcha to remove a reference when that's what your return statement returns. This behavior caused the following nasty bug for me: class Int { public: Int(int i) : m_int{i} {} int m_int; }; class C { public: C(Int obj) : m_obj{obj} {} const auto& getObj() {

慎用auto关键词

泄露秘密 提交于 2019-11-27 04:15:35
C++11 标准推出了一个新的关键词 auto ,这个关键词可以通过表达式自动推断返回值的类型,这也是新标准中被各编译器厂商支持最为广泛的特性之一。利用这个关键词可以有效减少代码的长度,特别是在使用模板元编程的时候。 举个简单的例子: vector < map < int , string >> stringMapArray; // 不使用 auto 版本 vector < map < int , string >>:: iterator iter1 = stringMapArray. begin (); // 使用 auto 版本 auto iter2 = stringMapArray. begin (); 看到这样简短的式子,我再也不能忍受第一个包含大量冗余信息的式子了。所以,最近写的C++里到处都是auto,恨不得在lambda的参数列表里也用(可惜不行)。 但是物极必反,auto的滥用却使一个非常隐蔽的问题悄然出现。最近写一个正则引擎的时候,发现运行效率总是低于预期,刚开始认为是动态内存分配的问题,通过替换成内存池后发现虽然效率有所提高,但是仍然达不到要求。于是想到了性能工具,一次检查下来发现如下语句竟然占用了70%的时间: // 函数声明 set< int >& getSet(); void foo() { // 占用 70% 的时间 auto s = getSet ()

C++11 Range-based for-loop efficiency “const auto &i” versus “auto i”

有些话、适合烂在心里 提交于 2019-11-27 04:13:20
问题 In C++11, I can iterate over some container like so: for(auto i : vec){ std::cout << i << std::endl; } But I know that this needlessly - needlessly , since I only need to print the values of vec - makes a copy of ( EDIT : each element of) vec , so instead I could do: for(auto &i : vec){ std::cout << i << std::endl; } But I want to make sure that the values of vec are never modified and abide by const-correctness, so I can do: for(const auto &i : vec){ std::cout << i << std::endl; } So my

SFINAE and decltype(auto)

隐身守侯 提交于 2019-11-27 03:49:27
问题 If a function template returns decltype(auto) (or another type specifier using auto ) but the return statement would be ill-formed, does SFINAE result? Is the return statement considered to be the immediate context of the function signature? Nothing in the N3690 draft seems to require this. By default, I guess SFINAE does not apply. This seems unfortunate because you can write a function to forward to another function, but you cannot make its existence conditional on the delegate as when

Using auto in loops c++

那年仲夏 提交于 2019-11-27 03:02:53
问题 I get warning signed/unsigned mismatch for the following code: auto n = a.size(); for (auto i = 0; i < n; i++) { } The problem is that by assigning 0 to i it becomes int rather than size_t . So what is better: size_t n = a.size(); for (size_t i = 0; i < n; i++) { } or this: auto n = a.size(); for (size_t i = 0; i < n; i++) { } or maybe you have a better solution? I like the first one more because it is bit more consistent, it just uses size_t rather than both size_t and auto for the same

undefined behaviour somewhere in boost::spirit::qi::phrase_parse

淺唱寂寞╮ 提交于 2019-11-27 02:26:48
I am learning to use boost::spirit library. I took this example http://www.boost.org/doc/libs/1_56_0/libs/spirit/example/qi/num_list1.cpp and compiled it on my computer - it works fine. However if I modify it a little - if I initialize the parser itself auto parser = qi::double_ >> *(',' >> qi::double_); somewhere as global variable and pass it to phrase_parse, everything goes crazy. Here is the complete modified code (only 1 line is modified and 1 added) - http://pastebin.com/5rWS3pMt If I run the original code and pass "3.14, 3.15" to stdin, it says Parsing succeeded, but with my modified

Does a declaration using “auto” match an extern declaration that uses a concrete type specifier?

狂风中的少年 提交于 2019-11-27 01:39:16
Consider the following program: extern int x; auto x = 42; int main() { } Clang 3.5 accepts it ( live demo ), GCC 4.9 and VS2013 do not ( live demo for the former ). Who is right, and where is the correct behavior specified in the C++ Standard? There's surprisingly little in the standard about this. About all we hear about redeclaration is: [C++11: 3.1/1]: A declaration (Clause 7) may introduce one or more names into a translation unit or redeclare names introduced by previous declarations. [..] and the only relevant part of auto 's semantics: [C++11: 7.1.6.4/3]: Otherwise, the type of the

C++11 新特性学习

一世执手 提交于 2019-11-27 00:31:31
在Linux下编译C++11 #include<typeinfo> int main() { auto a=10; cout<<typeid(a).name()<<endl; //得到a的类型,只是一个字符串 return 0; } 编译需要加-std=c++11,如下例: auto C++11中引入auto第一种作用是为了自动类型推导 auto的自动类型推导,用于从初始化表达式中推断出变量的数据类型。通过auto的自动类型推导,可以大大简化我们的编程工作 auto实际上实在编译时对变量进行了类型推导,所以不会对程序的运行效率造成不良影响 另外,似乎auto并不会影响编译速度,因为编译时本来也要右侧推导然后判断与左侧是否匹配。 auto a; // 错误,auto是通过初始化表达式进行类型推导,如果没有初始化表达式,就无法确定a的类型 auto i = 1; auto d = 1.0; auto str = "Hello World"; auto ch = 'A'; auto func = less<int>(); vector<int> iv; auto ite = iv.begin(); auto p = new foo() // 对自定义类型进行类型推导 auto不光有以上的应用,它在模板中也是大显身手,比如下例这个加工产品的例子中