草庐IT

base_ptr

全部标签

C++ 无法使用通用引用 move unique_ptr

考虑这段代码:templateTmov(T&&t){returnstd::move(t);}intmain(){std::unique_ptra=std::unique_ptr(newint());std::unique_ptrb=mov(a);}mov函数应该简单地接受一个通用引用并按值返回它,但是通过move而不是复制它。因此,调用此方法时不应涉及复制。因此,使用只能move的unique_ptr调用这样的函数应该没问题。但是,此代码无法编译:我收到错误:test.cpp:24:34:error:useofdeletedfunction‘std::unique_ptr::uniqu

c++ - 为什么 65537 不使用 CryptoPP 将 base64URL 编码为 "AQAB"?

我正在使用CryptoPP生成RSAkey对以允许对游戏服务器进行身份验证。我需要对我的公共(public)指数和模数进行base64URL编码以包含在JWK中,但遇到了一些问题。该代码显示了我如何生成RSAkey、提取指数并对其进行编码:typedefInvertibleRSAFunctionRSAPrivateKey;typedefRSAFunctionRSAPublicKey;RSAPrivateKeyprivateKey;privateKey.Initialize(rng,1024);RSAPublicKeypublicKey(privateKey);constInteger&

c++ - Range-Based for 循环如何处理临时容器

这个问题在这里已经有了答案:DoesaC++11range-basedforloopconditiongetevaluatedeverycycle?(1个回答)关闭7年前。假设这个例子:vectorget_vector();for(auto&v:get_vector()){...}get_vector()是否在每次迭代时重新计算?还是临时存储并评估一次?

c++ - 为什么 std::shared_ptr 的别名构造函数不初始化 std::enabled_shared_from_this?

考虑以下代码:structFoo:std::enable_shared_from_this{};structBar{Foofoo;};intmain(){std::shared_ptrbar_p(newBar);//makeshared_ptrtomemberwithaliasingconstructorstd::shared_ptrfoo_p(bar_p,&bar_p->foo);assert(bar_p->foo.shared_from_this());//fail!throwsbad_weak_ptr}不幸的是,它没有按预期工作(至少在GCC4.8.2中)。我查看了代码,似乎别名

c++ - 对象 std::shared_ptr 是否可以通过它的 std::weak_ptr 找到?

所以我有一个std::vector>myListOfT;我有一个std::weak_ptrptrToOneT;它是从用于填充该容器的指针之一创建的(假设我将它放在回调函数中)。威尔std::find在那个容器和我的weak_ptr给我一个原始shared_ptr的迭代器(如果集合中存在这样的一个)?它是在标准的某处得到保证还是依赖于此实现? 最佳答案 通过使用std::weak_ptr::owner_before,我们可以不锁定weak_ptr。我将使用比必要的稍微冗长的解决方案并引入owner_equal,它与std::owner

c++ - base-R seq 的 Rcpp 版本丢弃值

我写了一个Rcpp版本的base-Rseq函数。library(Rcpp)cppFunction('NumericVectorseqC(doublex,doubley,doubleby){//lengthofresultvectorintnRatio=(y-x)/by;NumericVectoranOut(nRatio+1);//computesequenceintn=0;for(doublei=x;i对于以下测试,它工作得很好。seqC(1,11,2)[1]1357911seqC(1,10,2)[1]1357911此外,它(有时)在传递带有十进制数字的值而不是整数。seqC(0.43

c++ - 将 unique_ptr 的 vector 传递给函数,const 引用

我通常会像这样传递一个包含原始指针的vector:someFunc(conststd::vector&classList){..}我想知道你是否可以像这样对unique_ptr做同样的事情:someFunc(conststd::vector>&classList){..}?它的意思是一样的吗?即只读。 最佳答案 是的,你可以。不,这不是同一件事:std::unique_ptr表示指针拥有的资源。通用原始指针可以具有许多其他语义。关于const的正确性,使用迭代器或operator[]访问vector的元素将产生对std::uniqu

c++ - boost::shared_ptr<string> 标准集

我有一组boost::shared_ptr,我希望它不是通过共享指针而是通过字符串来排序和唯一化的。我是否必须提供一个新的比较函数来获取共享指针并比较内容,或者已经存在我可以使用的比较器? 最佳答案 这非常具体,因此您可能需要一个自定义比较器。这应该有效:structpointercompare{booloperator()(constboost::shared_ptr&a,constboost::shared_ptr&b){return(*a)>(*b);}} 关于c++-boost:

c++ - 传递返回多态 unique_ptr 的 lambda 作为函数指针

我想传递一个非捕获的lambda,它返回一个std::unique_ptr,作为std::unique_ptr(*)()类型的函数指针.但是,这仅在我将lambda的返回类型明确声明为std::unique_ptr时才有效。.为什么要明确说明返回类型?为什么它适用于std::function没有这个额外的返回类型?#include#includestructBase{virtual~Base()=default;};structDerived:Base{};structFailsForF2{usingFunction=std::add_pointer_t()>;FailsForF2(F

c++ - 在带有 std::unique_ptr 的 lambda 中使用 std::bind

//Byconstl-valuereferenceautofunc2=std::bind([](conststd::unique_ptr>&pw)//fine{std::coutsize()>(22,1));//Bynon-constl-valuereferenceautofunc3=std::bind([](std::unique_ptr>&pw)//fine{std::coutsize()>(22,1));//ByValueautofunc4=std::bind([](std::unique_ptr>pw)//error{std::coutsize()>(22,1));func4(