草庐IT

make_them_different

全部标签

c++ - xvalues : differences between non class types and class types

考虑下面的最小示例:#includestructS{};intmain(){Ss;std::move(s)=S{};}它编译没有错误。如果我改为使用非类类型,则会收到错误。例如,以下代码无法编译:#includeintmain(){inti;std::move(i)=42;}枚举、作用域枚举等也是如此。错误(来自GCC)是:usingxvalue(rvaluereference)aslvalue这背后的原理是什么?我想这是对的,但我想了解我可以对除非类之外的所有类型执行此操作的原因是什么。 最佳答案 C++允许对类对象右值进行赋值,

c++ - shared_ptr 的静态成员函数 make_shared

使用libc++我在公共(public)部分找到了std::shared_ptr::make_shared()静态成员函数。当我已经为std::shared_ptr的特化定义了类型别名时,这非常方便:usingT=int;usingP=std::shared_ptr;autop=P::make_shared(123);//std::make_shared(123)static_assert(std::is_same::value);我担心标准合规性,因为来自可信来源的文章(1,2)没有提到std::shared_ptr的静态成员函数make_shared>.目前使用该功能是否不好?为什

c++ - enable_shared_from_this 和 make_shared 是否提供相同的优化

据我了解make_shared(...)可以提供一些内存分配优化(它可以在与类T的实例相同的内存块内分配引用计数器)。enable_shared_from_this是否提供相同的优化?所以:classT:std::enable_shared_from_this{};...autot=std::shared_ptr(newT);等同于:classT{};...autot=std::make_shared();如果不考虑sizeof(T)。 最佳答案 Doenable_shared_from_thisprovidesthesameopt

c++ - 采访 : what is the difference between pthread and windows thread created by _beginthread(ex)?

我在一次C++开发人员职位面试中被问到这个问题,这个问题的答案是什么? 最佳答案 我会说:IfIwantedtocreateaportablecross-platformC++binary,I'dusepthreadsandusethepthreadimplementationforwindows.IfIwantedtocreateawindows-specificC++binary,I'dusebeginthreadandavoidthe3rdpartydependencyonthepthreadlibrary.如果他们真的想知道

c++ - 使用 make_tuple 进行比较

这个问题在这里已经有了答案:关闭10年前。PossibleDuplicate:Implementingcomparisionoperatorsvia'tuple'and'tie',agoodidea?有时候我需要写一些丑陋的仿函数例如lhs.date_这让我很生气。所以我开始避免这样写:std::make_tuple(lhs.date_,lhs.time_,lhs.id_)我几乎很高兴,但请注意,我可能不是出于他们的目的使用元组让我担心。您能批评一下这个解决方案吗?或者这是一个很好的做法?您如何避免这种比较?更新:感谢您指出std::tie以避免复制对象。并感谢您指出重复的问题

c++ - 为什么 std::make_tuple 将 std::reference_wrapper<X> 参数转换为 X&?

在C++11标准中它声明(参见cppreference.com,另请参见标准的第20.4.2.4节)它声明templatetuplemake_tuple(Types&&...args);Createsatupleobject,deducingthetargettypefromthetypesofarguments.ForeachTiinTypes...,thecorrespondingtypeViinVtypes...isstd::decay::typeunlessapplicationofstd::decayresultsinstd::reference_wrapperforsome

iphone - 警告 : XXXX has different visibility (default) in YYYY and (hidden) in ZZZZ

我正在尝试制作一个使用OpenCV和另一个C++库的iPhone应用程序。它似乎可以很好地编译和链接。它确实有效。只是我想摆脱这个丑陋的警告:ld:warning:std::vector>::_M_insert_aux(__gnu_cxx::__normal_iterator>>,intconst&)hasdifferentvisibility(default)in/Users/nacho4d/Documents/Projects/iOS/iAR/opencv_device/lib/libcxcore.a(cxdatastructs.o)and(hidden)in/Users/nach

c++ - 如何将编译标志 -g 添加到 make 文件?

我有一个C++程序,其他人为其制作了一个make文件。我想用标志-g编译程序,但我不知道在哪里添加它。下面是make文件。CC=g++LOADLIBES=-lmCFLAGS=-Wall-O2SRC1=Agent.cppBreeder.cppCandidateSolution.cpp\Cupid.cppFateAgent.cppGrid.cppReaper.cpp\fitness.cppSRC2=main.cppSRC=$(SRC1)$(SRC2)OBJS=$(SRC1:.cpp=.o)AUX=$(SRC1:.c=.h)main:$(OBJS)#$(CC)$(CFLAGS)-o$(SRC

c++ - boost shared_ptr : difference between operator= and reset?

下面两段代码有区别吗?它们中的任何一个比另一个更可取吗?运算符=boost::shared_ptrfoo;//foo.ptrshouldbeNULLfoo=boost::shared_ptr(newBlah());//Involvescreationandcopyofashared_ptr?重置boost::shared_ptrfoo;//foo.ptrshouldbeNULLfoo.reset(newBlah());//foo.ptrshouldpointnowtoanewBlahobject注意:我需要定义shared_ptr然后将其设置在不同的行中,因为我在一段代码中使用它,例如

c++ - Lua C API : what's the difference between lua_gettop() and -1?

我不是很了解堆栈。lua_gettop()Returnstheindexofthetopelementinthestack.Becauseindicesstartat1,thisresultisequaltothenumberofelementsinthestack(andso0meansanemptystack).那么它和-1有什么区别呢?lua_getglobal(L,"Foo");if(lua_isfunction(L,lua_gettop(L))){lua_getglobal(L,"Foo");if(lua_isfunction(L,-1)){ 最佳