草庐IT

c++ - C++14 是否要求删除表达式必须调用 `void operator::delete(void*, std::size_t)` 而不是 `void::operator delete(void*)` ?

根据thisvoidoperatordelete(void*);(1)voidoperatordelete[](void*);(2)voidoperatordelete(void*,conststd::nothrow_t&);(3)voidoperatordelete[](void*,conststd::nothrow_t&);(4)voidoperatordelete(void*,std::size_t)(5)voidoperatordelete[](void*,std::size_t)(6)voidoperatordelete(void*,std::size_t,conststd:

c++ - 如何初始化 std::array<std::array<T, 2>, 2> 的对象?

我正在尝试初始化thing类型的对象:templatestructthing:std::array,2>{};thingt1{{{1,2},{3,4}}};我得到:error:nomatchingfunctionforcallto‘thing::thing()’thingt1{{{1,2},{3,4}}};同上thingt0{{1,2,3,4}};还有其他一些东西。 最佳答案 如果您使用的是C++17编译器,您只是少了一组额外的大括号。以下compiles:thingt1{{{{1,2},{3,4}}}};//||||-braces

c++ - 为什么这个使用 std::rotate 的简单程序无法编译?

这不工作:#includeintmain(){inta[]={1,2,3};autoit=std::rotate(std::begin(a),std::begin(a)+1,std::end(a));}我得到的错误是:main.cpp:6:10:error:variablehasincompletetype'void'autoit=std::rotate(std::begin(a),std::begin(a)+1,std::end(a));这显然是不正确的行为,因为rotate的声明是:templateForwardIteratorrotate(ForwardIteratorfirst

c++ - 我可以使用 std::bind 到 "attach"状态到函数吗?

我需要将std::function传递给某个算法。函数的类型是typedefstd::functionCondition;在最简单的情况下,这个函数看起来像这样boolsimpleCondition(constdouble&d){returnd现在我想传递相同的条件,但只有当条件连续多次满足时,函数才应返回true。我尝试了以下classRepeatingCondition{public:staticConditiongetRepeatingCondition(Conditionc,intreps){returnstd::bind(&RepeatingCondition::evalCo

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::move() 和 std::thread 不会编译

代码如下:#include#includeclassA{voidfoo(int&&arg)const{}voidboo()const{intvalue(0);std::threadt(&A::foo,this,std::move(value));t.join();}};intmain(){Aa;return0;}MSVisualStudio2012(工具集v110)给出下一个错误:errorC2664:'_Rxstd::_Pmf_wrap::operator()(const_Wrapper&,_V0_t)const':cannotconvertparameter2from'int'to

c++ - 为什么 std::thread 采用右值运行的函数?

std::thread有一件事我不明白:为什么std::thread的构造函数采用右值运行的函数?我通常想将具有一些成员的Functor运行到另一个线程。像这样:structFunction{voidoperator()(/*someargs*/){/*somecode*/}/*somemembers*/}voidrun_thread(){Functorf(/*somedata*/);std::threadthread(f,/*somedata*/);/*dosomethingandwaitforthreadtofinish*/}随着std::thread的当前实现,我必须确保我的对象

c++ - 为什么 COW std::string 优化在 GCC 5.1 中仍然启用?

根据GCC5发布更改页面(https://gcc.gnu.org/gcc-5/changes.html):Anewimplementationofstd::stringisenabledbydefault,usingthesmallstringoptimizationinsteadofcopy-on-writereferencecounting我决定检查一下并写了一个简单的程序:intmain(){std::stringx{"blah"};std::stringy=x;printf("0x%X\n",x.c_str());printf("0x%X\n",y.c_str());x[0]=

c++ - 使用 std::string 调用类方法

假设我有以下类(class):(可能是元生成的)classMyClass{public:myMethod();...}这里假设一些事情:1.)Ihavetheclassnamefromsomewhere(let'spretend)2.)Ihavethenamesoftheclassmethodssomewhere(std::map...perhaps?)所以...因为我可能直到运行时才知道myMethod()的名称,有没有办法使用std::string调用它?这是假设我在某处存储了一个类函数的名称。MyClassexample;std::stringfuncName{findMyMet

c++ - 如何将 std::ofstream 重定向到 std::cout?

我正在处理一些使用std::ofstream*类型的全局调试记录器的代码。我想将它重定向到std::cout,因为我正在实时使用代码,而不是为它设计的批处理方法。是否可以将它使用的全局std::ofstream*指针重定向到std::cout?我知道std::ofstream继承自std::ios,它允许使用rdbuf()方法更改流缓冲区,但是不幸的是,std::ofstream重新定义了rdbuf()方法,这使得以下代码无法编译:gOsTrace=newstd::ofstream();gOsTrace->rdbuf(std::cout.rdbuf());是否有另一种方法可以将gOsT