草庐IT

auto-scroll

全部标签

c++ - constexpr-if-else 主体能否在 constexpr auto 函数中返回不同类型?

我正在尝试编写一个函数,根据枚举的运行时值将值的枚举映射到一组类型。我意识到您不能根据枚举的运行时值返回不同的类型,因为编译器不知道要分配多少堆栈空间。但是,我正在尝试将其编写为constexpr函数,使用新的if-constexpr功能来实现它。我收到来自clang的错误,提示我使用了非法指定的模板参数。有人知道如何实现吗?编辑:这是一个更容易理解的版本,更简洁地展示了我的问题:http://coliru.stacked-crooked.com/a/2b9fef340bd167a8旧代码:#include#include#includenamespace{enumclassshape

c++ - auto_ptr 会防止这种情况发生吗?

我不太清楚auto_ptr在这种情况下是否会帮助我:classA{A(constB&member):_member(B){};...constB&_member;};AgenerateA(){auto_ptrsmart(newB());AmyA(*smart);returnmyA;}当smart离开其封闭范围时,myA._member引用是否有效?如果auto_ptr不是这里的答案,那是什么?编辑:我看到我把每个人都弄糊涂了;我必须在范围外返回myA,这就是为什么我关心_member在smart退出范围后是否有效。 最佳答案 这对你

【C++】基础知识讲解(引用、内联、auto,基于范围for循环)

 🌈个人主页:秦jh__https://blog.csdn.net/qinjh_?spm=1010.2135.3001.5343🔥 系列专栏:http://t.csdnimg.cn/eCa5z目录引用概念特性使用场景作参数作返回值传值、传引用效率比较引用和指针的区别内联函数概念查看方式特性 宏的优缺点 C++代替宏的技术前言    💬hello!各位铁子们大家好哇。       今日更新了引用、内联、auto,基于范围for循环的内容    🎉欢迎大家关注🔍点赞👍收藏⭐️留言📝引用概念引用不是新定义一个变量,而是给已存在变量取了一个别名,编译器不会为引用变量开辟内存空间,它和它引用的变量共用同

c++ - 如何使用 std::auto_ptr 声明动态数组?

我正在尝试声明一个动态int数组,如下所示:intn;int*pInt=newint[n];我可以用std::auto_ptr做到这一点吗?我试过类似的方法:std::auto_ptrpInt(newint[n]);但是它不编译。我想知道我是否可以使用auto_ptr构造声明一个动态数组,以及如何声明。谢谢! 最佳答案 不,你不能,也不会:C++98在数组方面非常有限,auto_ptr是一个非常笨拙的野兽,它通常不会做你需要的事情。您可以:使用std::vector/std::deque,或std::array,或者使用C++11和

c++ - 是否有 auto_ptr 的替代品可以与 c++11 中的 boost ptr_map 一起使用

在c++11中,auto_ptr已弃用,取而代之的是更合理的unique_ptr。唉,如果你使用boost::ptr_map,auto_ptr就完成了一个非常方便的用途:std::auto_ptrpLayer(newLayer());mRawLayerPtrMap.insert(layerName,pLayer);是否有可能使用与c++11类似的东西。这个我知道Layer*pLayer=newLayer();mFusedLayers.insert(fusedLayerName,pLayer);有效,但auto_ptr在一些更复杂的场景中有它的优点。是否有适用于C++11的替代品?

c++ - foreach(int i.. 和 foreach(auto i

我正在MacOX(LLVM4.2)附带的Clang编译器上试验C++11功能,以下结果让我感到困惑://clangcompilewith"c++-std=c++11-stdlib=libc++"#include#includeintmain(void){usingnamespacestd;vectoralist={1,2,3,4};for(inti=0;i在运行环境中,我得到如下不同的输出:12342340为什么我会得到不同的结果? 最佳答案 for(autoi:alist)这会获取alist中的每个value,因此i变为:1,2,

C++ : Different deduction of type auto between const int * and cont int &

这里是代码示例。a.intii=0;b.constintci=ii;c.autoe=&ci;-->eisconstint*d.auto&f=42;-->invalidinitializationofnon-constreferenceoftype‘int&’fromanrvalueoftype‘int’e.constauto&g=42-->ok观察:1.对于c)子句,自动推导类型const2.对于子句d),不会自动推导出类型const3.对于条款e),必须手动添加类型const才能使其工作。为什么子句c而不是d会自动推导类型const? 最佳答案

c++ - 'for(auto &str : vec)' 内部 for 循环的目的是什么?

我是C++的新手,正在尝试学习vector的概念。我在网上看到这段代码。我的问题是,'for(auto&str:vec)'中的内部for循环的目的是什么?为什么作者要对第一个引用(&str)创建第二个引用(&c)?intmain(){vectorvec;for(stringword;cin>>word;vec.push_back(word)){}for(auto&str:vec){for(auto&c:str){c=toupper(c);}}for(inti=0;i!=vec.size();++i){if(i!=0&&i%8==0)cout 最佳答案

c++ - 带有 if 语句的 auto 函数不会返回值

我制作了一个模板和一个auto函数,用于比较2个值并返回最小值。这是我的代码:#includeusingnamespacestd;//Templatewithavaluereturningfunction:PrintSmallertemplateautoPrintSmaller(TNumOne,UNumTwo){if(NumOne>NumTwo){returnNumTwo;}else{returnNumOne;}}intmain(){intiA=345;floatfB=23.4243;cout但它无法编译,我在VS2015上遇到此错误:错误C3487“int”:所有返回表达式必须推导出

c++ - 我可以使用 auto 或 decltype 代替尾随返回类型吗?

我发现尾随返回类型很容易定义返回复杂类型的函数的返回值,例如:autoget_diag(int(&ar)[3][3])->int(&)[3]{//usingtrailingreturntypestaticintdiag[3]{ar[0][0],ar[1][1],ar[2][2]};returndiag;}auto&get_diag2(int(&ar)[3][3]){//adding&autobecauseotherwiseitconvertsthearraytopointerstaticintdiag[3]{ar[0][0],ar[1][1],ar[2][2]};returndiag;