草庐IT

c++ - 指针上的运算符<(小于)是否一致?

注意:这个问题不是关于总顺序的。可以使用std::less获得相同类型指针的总顺序。.Accordingtothis,将两个指针与operator进行比较例如,如果它们指向不同的分配,则不允许。在什么意义上不允许?它是实现定义的、未指定的还是未定义的行为?我想我在某处读到它未指定。不需要实现来记录行为是什么,但必须有一些行为。所以这意味着,比较任意两个指针仍然是合法的,但不一定会产生总顺序。这是否意味着,当比较相同的两个指针两次时,我们仍然必须获得一致的结果?一般情况是:在应用程序中两次调用相同的未指定行为是否总是产生相同的结果?inti1,i2;int*a=&i1;int*b=&i2

c++ - optional<double> 可以实现为 8 字节对象吗?

是否可以实现std::optional这样sizeof(std::optional)==8通过某种方式使用它可以将字符存储在NAN中,请参阅http://en.cppreference.com/w/cpp/numeric/math/nan?有实现吗?可以根据标准中提供的功能来完成吗? 最佳答案 我不认为这是可以做到的,因为没有规则阻止程序自己利用和依赖NaN中的额外位。然后,如果您将魔数(MagicNumber)存储到optional中,它看起来就像不存在,而不是应用程序的特殊NaN。

c++ - 我可以在不复制的情况下将 std::vector<char> 重新解释为 std::vector<unsigned char> 吗?

我引用了std::vector我想用作接受std::vector的函数的参数.我可以不复制就这样做吗?我有以下功能并且有效;但是我不确定是否真的发生了复制-有人可以帮助我理解这一点吗?是否可以使用std::move以避免复制还是它已经没有被复制?staticvoidshowDataBlock(boolusefold,boolusecolor,std::vector&chunkdata){char*buf=chunkdata.data();unsignedchar*membuf=reinterpret_cast(buf);std::vectorvec(membuf,membuf+chun

c++ - std::promise<T> 是线程安全的吗?

它安全吗,就像std::mutex的情况一样对于std::promise待制mutable,还是取决于T?如:usingData=std::tuple;structX{std::futureprepare()const{returnm_promise.get_future();}voidasyncHandler(inta,intb)const{m_promise.set_value({true,a,b});}voidcancel()const{m_promise.set_value({false,0,0});}mutablestd::promisem_promise;//Isthiss

c++ - 使模板类型更具体(T => Certain<X>)以帮助内容辅助

如何使模板类型在内部更具体以帮助内容辅助?templateclassB{//note:inrealcase,ithasmoretemplateparameterpublic:voidf(){}};templateclassC{//".B1*b;voidtest(){b->^ctrl+spacedoesn'tshowf()}};我糟糕的解决方法是在类中创建模板特化C,但它会以另一种方式混淆内容辅助。下面是另一种解决方法,但它非常乏味。我必须在B反射(reflect)模板参数并在C处使用此类反射(reflect)一个接一个。templateclassB{public:usingreflec

c++ - 检查类型是否可以作为 boost::lexical_cast<string> 的参数

我有以下特征类(IsLexCastable)来检查是否可以通过调用boost::lexical_cast将类型转换为字符串.它错误地返回true对于vector.#include#include#include#include#include#includeusingnamespacestd;usingnamespaceboost;namespacestd{///AddingtostdsincethesearegoingtobepartofitinC++14.templateusingenable_if_t=typenamestd::enable_if::type;}templates

c++ - 我是否应该在我的头文件中包含 <stdio.h>,这样我就可以声明一个接受 FILE* 的函数?

例如在foo.h中:typedefstructfoo_tfoo_t;/*Lotsoffunctiondeclarationsdealingwithfoo_t...*/intfoo_print(constfoo_t*foo);/*Printfootostdout.*/intfoo_fprint(FILE*f,constfoo_t*foo);/*Printfootofilef.*/我不想在foo.h中乱放太多foo.h的用户可能不想包含的其他头文件,但我确实需要声明采用FILE*等类型的函数。我怀疑我是第一个遇到这种困境的人,那么在这种情况下人们通常会做什么呢?还是我想避免在我的头文件中包

c++ - 无法将左值绑定(bind)到 A<Cv2>&&

我认为通用引用(T&&)应该采用任何类型的引用。但以下内容不起作用。当我尝试在我正在编写的库中保持const-correct时,我遇到了这个问题。我是C++的新手,以前从未见过这样的东西。测试.cpp:enumCv_qualifier{constant,non_const};templateclassA;templateclassA{public:templatevoidt(constA&&out){}};templateclassA{public:templatevoidt(constA&&out){}};intmain(){Aa;Ab;a.t(b);}错误(使用g++test.cp

c++ - vector<double> 比 double* : why? 快

这是我用std::vector试过的一个循环和普通的旧double*.对于1000万个元素,vector版本的运行时间是double*的大约80%。版本需要;对于N的几乎任何值,vector明显更快。查看GCCSTL源代码,我没有看到std::vector正在做任何比double*本质上更有趣的事情习语正在做(即,使用普通旧new[]分配,operator[]取消引用偏移量)。Thisquestion也是这么说的。知道为什么vector版本更快吗?Compiler:GCC4.6.1Examplecompileline:g++-Ofast-march=native-DNDEBUG\-ft

c++ - std::vector< std::vector<unsigned char>> 或 std::deque< std::vector<unsigned char>>?

我有一个现有的算法,如果可能的话,我需要稍微优化它。目前无法在此算法中进行大量更改。该算法适用于std::vector>的实例.它看起来像这样:typedefstd::vectorinternal_vector_t;std::vectorinternal_vectors;while(fetchinglotsofrecords){internal_vector_ttmp;//reads1Mbofcharsintmp...internal_vectors.push_back(tmp);//somemorework}//usethisinternal_vectors算法在internal_v