草庐IT

make_function

全部标签

c++ - 为什么 'control reaches end of non-void function' 只是一个警告?合法吗?

这个问题在这里已经有了答案:WhydoesthisC++snippetcompile(non-voidfunctiondoesnotreturnavalue)[duplicate](7个答案)关闭8年前。C++定义具有非void返回类型的函数允许控制到达函数末尾而不是到达return语句是否合法?gcc和clang仅为此发出警告。这样做的代码是合法的还是这些编译器只是慷慨?海湾合作委员会:warning:noreturnstatementinfunctionreturningnon-void[-Wreturn-type]clang:warning:controlreachesendof

c++ - Make_shared - 自己的实现

我正在尝试自己实现shared_ptr。make_shared有问题。std::make_shared的主要特点是它在连续的内存块中分配计数器block和对象。我怎样才能做同样的事情?我试过这样做:templateclassshared_ptr{private:class_ref_cntr{private:longcounter;public:_ref_cntr():counter(1){}voidinc(){++counter;}voiddec(){if(counter==0){throwstd::logic_error("alreadyzero");}--counter;}long

c++ - 就地构建 std::function 目标

我所理解的std::function的典型用法#include#includeusingnamespacestd;classC{public:C(){coutf=C();f();return0;}产量如下输出CREATINGMOVECDELETINGCALLINGDELETING显然,临时对象是在堆栈上创建的,然后移入函数对象。如果未提供移动构造函数,则复制它。是否有无需临时对象即可设置目标的标准方法? 最佳答案 function由任何仿函数Ff构造的方式由§20.9.11.2.1中的标准规定为(假设f是一个非空值,强调我的):*t

c++ - 将 std::function<int(int)> 赋值给 std::function<const int&(const int& x)>

以下代码编译但在VC++2015(发行版)中产生未定义的输出和运行时错误othercompilers.#include#includeintmain(){std::functionf=[](intx){returnx;};std::functiong=f;std::cout为什么赋值g=f;是允许的? 最佳答案 考虑重写等效代码以避免lambda或std::function:intf(intx){returnx;}intconst&g(intconst&x){returnf(x);}这是一个结构良好的代码,尽管如此,它仍会返回一个对

c++ - 从 Qt 5.6 切换到 Qt 5.7 - 命名空间 std 中的 "no member ' make_unique'

我有一个CMakeQt项目,它使用了多个c++14功能,包括std::make_unique。通常这将通过以下方式处理:LIST(APPENDCMAKE_CXX_FLAGS-std=c++14)或ADD_COMPILE_OPTIONS(-std=c++14)我想将项目从5.6版升级到5.7版,但在测试构建期间出现多次失败并出现错误nomember'make_unique'innamespacestd我已验证所有适当的header和编译选项都已到位,并排除了任何环境问题。使用Qt5.7绝对是个问题。有什么解决方法吗? 最佳答案 原来这

c++ - 有什么方法可以欺骗 std::make_shared 使用默认初始化吗?

您应该使用std::make_shared确保带有计数器的block存储在数据旁边。不幸的是内部std::make_shared对T使用零初始化(即使用T()来初始化数据block)。有什么办法可以让它使用默认初始化吗?我知道我可以使用std::shared_ptr(newT,[](autop){deletep;}),但我最终会在这里进行两次分配(数据和计数器block不会彼此相邻)。 最佳答案 创建一个派生类来执行简单的构造。structD:T{D(){}//Non-trivialconstructor.Default-initi

c++ - reference_wrapper : make_pair VS Class Template Argument Deduction (CTAD)

为什么make_pair和类模板参数推导(CTAD)不同意生成哪种类型?#include#include#include#includeintmain(){intmyInt=5;std::reference_wrappermyIntRef=myInt;automyPair=std::make_pair(myInt,myIntRef);std::pairMy2ndPair(myInt,myIntRef);std::cout输出:St4pairIiRiE//std::pairSt4pairIiSt17reference_wrapperIiEE//std::pair>更新:为什么std::p

采用依赖于模板参数的 std::function 的 C++11 模板函数

我正在尝试编写一个接受依赖于模板参数的std::function的模板函数。不幸的是,编译器无法正确推导出std::function的参数。这里有一些简单的示例代码:#include#includeusingnamespacestd;voidDoSomething(unsignedident,unsignedparam){coutvoidCallFunc(Identident,Paramparam,std::functionop){op(ident,param);}intmain(){unsignedid(1);unsignedparam(1);//Thefollowingfailst

c++ - 未定义对 'function' 的引用——链接器问题?

我目前正在尝试在Linux终端上编译和链接我的C++文件。我运行的命令是:g++-ogameplaygamePlay.cppplayer.cppmain.cppdisplay.cpp-lcurses该命令似乎可以完美地编译所有内容,但是一旦它尝试链接内容,我就会遇到2个错误。undefinedreferenceto'gamePlay::deal(std::vector>,std::vector>)'undefinedreferenceto'gamePlay::score(player)'下面是我的gamePlay.CPP文件。我真的迷路了,非常感谢任何帮助!#include"gameP

c++ - std::reverse_copy "error: function call has aggregate value"

#include#include#include#includeusingnamespacestd;intmain(){intarrA[]={1,2,3,4,5,6,7,8,9};vectorvecIntA(arrA,arrA+sizeof(arrA)/sizeof(arrA[0]));vectorvecIntB(vecIntA.size());//copy((vecIntA.rbegin()+3).base(),(vecIntA.rbegin()+1).base(),vecIntB.begin());//OKvector::iterators=(vecIntA.rbegin()+3)