草庐IT

function-constructor

全部标签

c++ - 在 std::function 上强制执行 "noexcept"?

此代码编译并运行,抛出int:#includevoidr(std::functionf){f();}voidfoo(){throw1;}intmain(){r(foo);}但是我希望编译器拒绝r(foo);行,因为r应该只传递一个noexcept函数。noexcept说明符似乎被忽略了。有什么办法可以实现吗?编辑:这个问题不同于Isknowledgeaboutnoexcept-nesssupposedtobeforwardedwhenpassingaroundafunctionpointer?因为我要求补救措施,特别是在std::function的情况下。

c++ - 为什么 std::function 构造函数或赋值之间存在差异?

std::function类型删除构造函数定义为:templatefunction(Ff);赋值运算符定义为:templatefunction&operator=(F&&f);(来源cppreference)为什么构造函数通过值获取f,而operator=通过转发引用获取f? 最佳答案 我只能猜测,但我猜这是因为它被添加到C++而右值引用和转发引用被添加到语言中。因此其API的某些部分获得了转发引用,而另一些则没有。有一个小优点:如果F的复制构造函数可以扔而移动不能,std::function(F)可以保证不抛出,而std::fun

c++ - 带有 C++ 模板的虚假 "use of local variable with automatic storage from containing function"?

以下代码无法在g++7.2.0中编译templateclassRequest{intcontent=0;public:friendvoidsetContent(inti,void*voidptr){Request*ptr=(Request*)voidptr;ptr->content=i;}intgetContent(){returncontent;}};intmain(){Requestreq;setContent(4,&req);returnreq.getContent();}有错误test.cpp:Ininstantiationof‘voidsetContent(int,void*

c++ - has_trivial_default_constructor 是如何工作的?

谁能给我解释一下has_trivial_default_constructor是如何工作的?我试图在提升实现中找到它,但不幸的是宏太多,我迷路了......如何使用模板检测C++中的trivial_default_constructor?我需要一个C++03而非11的示例。#include#includestructA{A(){}inta;//std::vectorb;};intmain(intargc,char*argv[]){structB{std::vectorb;};boolresult=boost::has_trivial_default_constructor::value

c++ - 错误 : base class 'A1' has private copy constructor

在windows平台上使用Clang3.7见以下代码:classA1{public:A1(char*name){}virtual~A1(){}private:A1(constA1&){}};classB1:publicA1{public:B1():A1(""){}};我收到以下错误:MyFile(31):8:error:baseclass'A1'hasprivatecopyconstructorB1():A1(""){}^MyFile(25):2:note:declaredprivatehereA1(constA1&){}^公开A1复制构造函数,消除错误!这里发生了什么?注意:通过改变

C++:莫名其妙的 "pure virtual function call"错误

我在使用MicrosoftVisualC++2015时遇到了一些困难,但能够用一个小程序重现该问题。给定以下类:classBaseClass{public:BaseClass():mValue(0),mDirty(true){}virtual~BaseClass(){}virtualintgetValue()const{if(mDirty)updateValue();returnmValue;}protected:virtualvoidupdateValue()const=0;mutableboolmDirty;mutableintmValue;};classDerivedClass:

c++ - "function"类型的优点是什么(不是 "pointer to function")

阅读C++标准,我看到有“函数”类型和“函数指针”类型:typedefintfunc(int);//functiontypedefint(*pfunc)(int);//pointertofunctiontypedeffunc*pfunc;//sameasabove我从未见过在示例之外使用的函数类型(或者我可能没有意识到它们的用法?)。一些例子:funcincrease,decrease;//declarestwofunctionsintincrease(int),decrease(int);//sameasaboveintincrease(intx){returnx+1;}//cann

c++ - std::thread constructor 传递指针和传递ref有区别吗?

创建调用成员函数的线程时,传递当前类的指针和传递引用有区别吗?从下面的示例中,方法1的行为是否与方法2相同?有什么区别吗?classMyClass{public:MyClass(){};~MyClass(){};voidmemberFunction1(){//method1std::threadtheThread(&MyClass::memberFunction2,this,argumentToMemberFunction2)//method2std::threadtheThread(&MyClass::memberFunction2,std::ref(*this),argumentT

c++ - gcc 与 clang、msvc 和 icc : Is this function call ambiguous?

我能得到的所有编译器都同意这很好:templateautofoo(Check,T...)->void;templateautofoo(int,T...)->void;intmain(){foo(7,"");}但是,根据gcc,以下代码(带有不能从函数参数推导的前导模板参数)是不明确的:templateautobar(Check,T...)->void;templateautobar(int,T...)->void;intmain(){bar(7,"");//ambiguousaccordingtogccbar(7);//justfine}另一方面,clang、msvc和icc对此非常满

c++ - 使用 c++1z 在 <functional> 上出现 clang 4 构建错误

我刚刚将我的archlinux系统更新到最新版本,其中包括gcc7.1.1。尝试构建这个:#includeintmain(intargc,char**argv){return1;}使用命令clang++main.cpp-std=c++1z导致错误:Infileincludedfrommain.cpp:1:Infileincludedfrom/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/7.1.1/../../../../include/c++/7.1.1/functional:60:Infileincludedfrom/usr/bin/../lib