草庐IT

this-reference

全部标签

c++ - std::optional<std::reference_wrapper<T>> - 可以吗?

是std::optional>是否符合C++17的标准(或草案)?标准明确指出,引用类型的std::optional格式错误。但它是否包括reference_wrapper? 最佳答案 是的。那没问题。它不包括reference_wrapper因为reference_wapper不是引用类型。只有实际的引用类型是不允许的。 关于c++-std::optional>-可以吗?,我们在StackOverflow上找到一个类似的问题: https://stackov

c++ - shared_ptr 中对 const int 的 undefined reference

我有一个配置类//config.hppclassConfig{public:staticconstexprinta=1;staticconstexprintb=1;}并包含在main.cpp中//main.cpp#include"config.hpp"intmain(){std::coutstream=std::make_shared(Config::a);//compileerror}编译器说未定义对Config::a的引用它在使用cout时有效,但在shared_ptr构造函数中时无效。我不知道为什么会这样。 最佳答案 请注意s

C++11 std::this_thread::sleep_until() 在使用 GCC 4.8.5 编译时挂起

考虑以下程序:#include#includeintmain(){std::this_thread::sleep_until(std::chrono::steady_clock::now()-std::chrono::seconds(10));return0;}当用GCC4.8.5编译时,它会挂起。用GCC4.9及以上或clang3.4及以上编译时,立即返回,为什么会挂?据我了解,GCC4.8.5完全支持C++11标准。 最佳答案 这是一个已确认的错误,已在gcc4.9中修复。https://gcc.gnu.org/bugzilla

c++ - 当我尝试编译时带有 boost 的 undefined reference

当我尝试使用boost编译我的服务器时,我遇到了很多错误。在这里,这是我的生成文件:NAME=serveurSRCS=Serveur/main.cpp\Serveur/Client.cpp\Serveur/Commande.cpp\Serveur/My_exception.cpp\Serveur/Network.cpp\Serveur/Server.cpp#####################################################OBJS=$(SRCS:.cpp=.o)CC=g++RM=rm-fCFLAGS=-g-W-Wall-WerrorINCL=./S

c++ - decltype - "the only context in which a variable defined as a reference is not treated as a synonym for the object to which it refers"?

我正在阅读C++Primer,第5版,第1页。71他们首先给出了这个代码示例:constintci=0,&cj=ci;decltype(ci)x=0;decltype(cj)y=x;decltype(cj)z;//error然后他们说:Itisworthnotingthatdecltypeistheonlycontextinwhichavariabledefinedasareferenceisnottreatedasasynonymfortheobjecttowhichitrefers.这是什么意思?我不明白。y指的是x。那么有什么收获呢? 最佳答案

C++11 原子 : why does this code work?

让我们采用这个结构:structentry{atomicvalid;atomic_flagwriting;charpayload[128];}两个线程A和B以这种方式同时访问这个结构(让e成为entry的一个实例):if(e.valid){//dosomethingwithe.payload...}else{while(e.writing.test_and_set(std::memory_order_acquire));if(!e.valid){//writee.payloadonebyteatatime//(thepayloadwrittenbyAmaybedifferentfrom

C++ 错误 : reference to non-static member function must be called

我正在尝试创建一个类来抽象libuv网络功能的一些基本行为。#defineTCP_BACKLOG256class_tcp{uv_tcp_t*tcp=NULL;public:~_tcp(){deletetcp;}voidlisten_uv_listen_uv_connection_cb(uv_stream_t*stream,intstatus){printf("NEWCONNECTION\n");}voidlisten(constchar*host,intport){tcp=newuv_tcp_t();uv_tcp_init(uv_default_loop(),tcp);sockaddr

c++ - 调试 :FASTLINK - What is this error?

#include#include#includeusingnamespacestd;intmain(){FILE*fPtr=fopen("english.txt","r");if(fPtr==NULL){coutfreq;while(!feof(fPtr)){fscanf(fPtr,"%s",word);freq[word]++;}multimapfreq_rev;map::iteratorit;for(it=freq.begin();it!=freq.end();it++)freq_rev.insert(make_pair(it->second,it->first));multima

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

c++ - 如何将 'this' 作为参数传递给另一个没有循环依赖的类构造函数?

我特别想到策略模式(设计模式,GoF94),其中建议传递给策略构造函数的上下文可以是包含策略(作为成员)本身的对象。但以下内容不起作用://analysis.hclassStrategyBase;classStrategy1;classStrategy2;classAnalysis{...voidChooseStrategy();private:StrategyBase*_s;...};//analysis.cppvoidAnalysis::ChooseStrategy(){if(...)_s=newStrategy1(this);elseif(...)_s=newStrategy2(