草庐IT

non-capturing

全部标签

c++ - 为什么不能实例化带有 "non const"复制构造函数的对,而没有实例化一对是可能的?

假设您有以下类(class):structA{A(){}A(A&)=delete;};intmain(){std::pairp1;return0;}以下代码将无法编译(使用-std=c++11和g++)并出现以下错误:/usr/include/c++/5/bits/stl_pair.h:Ininstantiationof‘structstd::pair’:test.cpp:13:23:requiredfromhere/usr/include/c++/5/bits/stl_pair.h:127:17:error:‘constexprstd::pair::pair(conststd::pa

c++ - 错误 : invalid initialization of non-const reference of type ‘bool&’ from an rvalue of type ‘std::vector<bool>::reference {aka std::_Bit_reference}’

为什么我会收到错误:从类型为“std::vector::reference{akastd::_Bit_reference}”的右值对类型为“bool&”的非常量引用进行无效初始化?vector>vis;bool&visited(intx,inty){returnvis[x][y];//error}据我所知,vector中的operator[]返回引用,所以它应该是一个左值,但它不起作用。我应该怎么做才能让它发挥作用? 最佳答案 那是因为std::vector不是它看起来的样子。std::vector有一个特化与类型bool-它是空间

c++ - Lambda Capture by Value 强制所有作用域对象为 const

我打算用C++编写一个内存模式,结果采用了以下方法std::functionMemoize(std::functionfn){std::mapmemo;std::functionhelper=[=](intpos){if(memo.count(pos)==0){memo[pos]=fn(pos);}returnmemo[pos];};returnhelper;}奇怪的是,我的编译器VS2012,拒绝编译并出现以下错误1>Source1.cpp(24):errorC2678:binary'[':nooperatorfoundwhichtakesaleft-handoperandoftyp

c++ - 铿锵错误 : non-type template argument refers to function that does not have linkage -- bug?

我有一些非常简单的(C++11)代码,最新的clang(version3.4trunk187493)无法编译,但GCC编译正常。代码(下面)实例化函数模板foo使用局部函数类型Bar然后尝试将其地址用作类模板Func的非类型模板参数:templatestructFunc{};templateexterninlinevoidfoo(){usingFoo=Func>;}intmain(){structBar{};//function-localtypefoo();return0;}clang发出以下错误:error:non-typetemplateargumentreferstofunct

c++ - 重载运算符 : const vs non-const return type : any difference of performance?

如果我们去维基百科article关于C++运算符,我们有一个例子:Addition:a+b->TT::operator+(constT&b)const;因此运算符返回类型为T的非常量。如果我们看这个guideline作者说返回类型应该是const以避免以下语法:(a+b)=c现在假设我不介意这种语法,并考虑a和b是大数组。从“纯”性能的角度来看,返回类型中缺少const关键字是否会阻止编译器的优化(g++和带有-O3的英特尔icpc)?如果答案是"is",为什么? 最佳答案 这是一个有趣的问题。在C++03中,没有更好的机会使用这两

c++ - Lambda 捕获列表 : capturing object's member field by value not possible without capturing the whole object?

下面的代码voidCMainWindow::someMethod(constCLocationsCollection&parentItem){autof=[this,parentItem.displayName](){};}给我一​​个错误:errorC2143:syntaxerror:missing']'before'.'如果我想通过ref捕获parentItem.displayName,我会为它创建一个非依赖别名标识符:constQString&name=parentItem.displayName;autof=[this,&name](){};//Orshoulditbe[thi

c++ - 在 lambda 表达式中,通过 [&captured] 和 [&local = captured] 捕获有什么区别?

vectorvec;//aautofoo=[&vec](){//dosomething};//bautofoo=[&v=vec](){//dosomething};我是否正确理解a和b之间的唯一区别是在b情况下为“vec”创建别名“v”还是还有更多? 最佳答案 在这种情况下没有真正的区别。但是,如果您按值(value)捕获,则会有所不同:conststd::vectorvec;//noteconstautofoo=[vec]()mutable{//can'tchangevecheresinceitiscapturedwithcv-q

c++ - 处理 "depends on non-NOTIFYable properties"警告

我有一个暴露给QML的C++对象,它具有“某种”只读属性,除了该属性仍然需要从QML设置,所以它定义了一个WRITE方法,但是除了初始的ma​​ndatory设置它永远不会改变,所以我觉得NOTIFY是多余的,因为它在使用时已经设置了该值,并且它永远不会改变。但是,QML不同意我的感受,并且无论如何它都会发出“表达式取决于不可通知​​的属性”警告。由于使用属性实例化对象的方式是设置该值的唯一适用方式,因此不可能使用可调用的setter,因为这样会要求对象已经“完成”并且没有它就无法真正完成那个值。因此需要属性机制和WRITE方法,不幸的是,这导致Qt相信属性会改变。我尝试将该属性设置为

c++ - SIMD/SSE : How to check that all vector elements are non-zero

我需要检查所有vector元素是否非零。到目前为止,我找到了以下解决方案。有一个更好的方法吗?我在Linux/x86_64上使用gcc4.8.2,指令高达SSE4.2。typedefcharChrVect__attribute__((vector_size(16),aligned(16)));inlinebooltestNonzero(ChrVectvect){constChrVectvzero={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};return(0==(__int128_t)(vzero==vect));}更新:上面的代码被编译为以下汇编代码(当编译为非

c++ - 将 NON-POD 类型传递给 Variadic 函数是未定义的行为?

Inthisdocument,作者说OnlyaPOD-typecanbeanargumentfortheellipsis"..."whilestd::stringisnotaPOD-type.我将此理解为将NON-POD类型传递给Variadic函数是未定义的行为。对吗?不过,他是在说C/C++标准吗?我试图在n3242C++规范中找到它。但是找不到。我想知道我的理解是否正确,这是一个标准。 最佳答案 它在C++115.2.2/7中指定:Passingapotentially-evaluatedargumentofclasstype