草庐IT

auto_increment_increment

全部标签

C++关键词auto详解

顾得泉:个人主页个人专栏:《Linux操作系统》 《C++从入门到精通》  《LeedCode刷题》键盘敲烂,年薪百万!一、小思考           随着我们对于C++的不断学习,遇到的程序越来越复杂,程序中用到的类型也越来越复杂,经常体现在:        1.类型难于拼写        2.含义不明确导致容易出错举个栗子来说:#include#includeintmain(){std::mapm{{"apple","苹果"},{"orange","橙子"},{"pear","梨"}};std::map::iteratorit=m.begin();while(it!=m.end()){//

c++ - 什么时候允许编译器优化 auto+brace 样式初始化?

假设您有一个名为Product的类,定义如下:classProduct{public:Product(constchar*name,inti);Product(Product&&rhs);Product(constProduct&rhs);~Product();private:constchar*m_name;intm_i;};然后你像这样初始化一个变量:autop=Product{"abc",123};我认为标准规定编译器必须在逻辑上执行以下操作:构建一个临时产品移动构建p(使用临时Product)但是允许编译器对其进行优化,以便直接构造p。我验证了这一点(VisualStudio2

c++ - 避免使用 auto 关键字字面上重复 const 和非常量的代码?

好的,我做了一些研究,显然在这个主题上有很多重复的问题,仅举几例:Elegantsolutiontoduplicate,constandnon-const,getters?Howtoavoidoperator'sormethod'scodeduplicationforconstandnon-constobjects?HowdoIremovecodeduplicationbetweensimilarconstandnon-constmemberfunctions?等但我还是忍不住再次提出来,因为与c++14auto类型的返回值,我实际上是在复制函数体,唯一的区别是const函数限定符。c

c++ auto 不命名类型

我在我的代码中使用关键字auto137autoi=boost::find(adresses,adress);在使用以下命令编译时出现这些错误[vickey@tbtests]$clear;g++testCoverDownloader.cpp../CoverDownloader.cpp-I/usr/include/QtGui/-I/usr/include/QtCore/-lQtGui-lQtCore-std=c++0x../CoverDownloader.cpp:137:10:error:‘i’doesnotnameatype../CoverDownloader.cpp:139:8:err

c++ - 'auto' 关键字如何知道何时使用 const_iterator 匹配函数重载?

我了解thisquestion的内容但是当使用函数重载时,事情是如何工作的呢?例如在std::map中定义了以下方法:iteratorfind(constkey_type&k);const_iteratorfind(constkey_type&k)const;如何使用auto关键字来选择一个或另一个?以下内容对我来说似乎不正确:autoi=mymap.find(key);//callsthenon-constmethod?constautoi=mymap.find(key);//callstheconstmethod? 最佳答案 s

c++ - 如何在 C++ 中使用 BOOST_AUTO 模拟 'const auto'?

使用BOOST_AUTO宏,我们可以模拟C++11之前不可用的auto关键字:BOOST_AUTO(var,1+2);//intvar=3autovar=1+2;//thesameinC++11有没有办法模拟constauto?constautovar=1+2;//constintvar=3 最佳答案 您可以只包含“尾随”常量:#includeintmain(){BOOST_AUTO(constx,42);static_assert(std::is_const(),"weehoo");}出于多种原因,尾部位置是const限定符唯一一

c++ - 什么时候应该使用 const_iterator 而不是 auto

下面是一个例子,我认为它说明了使用const_iterator比使用“constauto”更可取的情况。这是因为容器不提供cfind()函数。还有其他选择吗?或者应该使用“constauto”而忽略const的缺失?std::stringGetJobTitle(conststd::string&employee){usingEmployeeTitles=std::unordered_map;EmployeeTitlesemployees={{"Alice","Director"},{"Bob","Manager"},{"Charlie","Developer"}};//Option1.

c++ - 自动将 c++11 auto 关键字重写为派生类型

也许是个奇怪的问题,但是有没有可用的软件,给定一堆c++11代码,派生所有类型的自动类型变量并用这些派生类型重写代码?还有初始化列表?原因是我们想提供我们代码的向后兼容版本(非C++11),主要是为了与osx的可移植性。自动输入和初始化列表是我们最常使用的功能,因为它们使代码更具可读性,但手动删除它们是不行的。由于这实际上是编译器对自动类型化变量所做的,所以它似乎并不太牵强? 最佳答案 查看BOOST_AUTO和/或BOOST_TYPEOFhttp://www.boost.org/doc/libs/1_48_0/doc/html/t

c++ - auto stdMaxInt = std::max<int> 的类型推导失败;

使用GCC4.8.4和g++--std=c++11main.cpp输出以下errorerror:unabletodeduce‘auto’from‘max’autostdMaxInt=std::max;对于这段代码#includetemplateconstT&myMax(constT&a,constT&b){return(a;myMaxInt(1,2);autostdMaxInt=std::max;stdMaxInt(1,2);}为什么它适用于myMax但不适用于std::max?我们可以让它与std::max一起工作吗? 最佳答案

c++ - constexpr if with initializer 由标准保证吗? 'constexpr(constexpr auto x = f(); x) { }'

我找不到任何关于新C++17if初始化语法的信息和“constexprif”在:http://open-std.org/JTC1/SC22/WG21/docs/papers/2016/p0128r1.html不过,Clang-HEAD支持该语法...constexprautof(){returntrue;}intmain(){ifconstexpr(constexprautox=f();x){}}在线代码在这里->http://melpon.org/wandbox/permlink/dj3a9ChvjhlNc8nr是constexprif带有标准保证的初始值设定项,如constexpr