草庐IT

c++ - float 的类内静态 const 初始化与 C++ 中的 int 有何不同?

我有一个包含staticconst成员的类,我正在类声明中对其进行初始化:#includeclassFoo{public:staticconstinti=9;staticconstfloatf=2.9999;};intmain(){std::cout当使用带有选项--std=c++11的GCC4.8.2编译时,它给出了这个编译错误:foo.cpp:7:32:error:‘constexpr’neededforin-classinitializationofstaticdatamember‘constfloatFoo::f’ofnon-integraltype[-fpermissive]

c++ - 是否有任何 c/c++ 编译器可以警告(或给出错误)或枚举转换为 int?

清理使用硬编码整数文字而不是枚举的旧c/c++代码,找到函数声明已被正确重构而不是正文的地方是乏味的。例如enumimportant{little=1,abit=2,much=3};voidblah(inte){//magicstuffhere}voidboing(inte){...}voidguck(importante){switch(e){case3://thiswouldbeagoodplaceforawarningblah(e);//andthisbreak;default:boing((int)e);//butthisisOK(althoughimperfectandawa

未签名的int到字节

我正在尝试创建一个自定义输入流。我的问题是,read()方法从0-255返回整数,但是我需要将其转换为字节,解密它,然后将其转换回整数。如何?我需要类似的东西:InputStreamin=...;OutputStreamout=...;intunsigned=in.read();bytesigned=unsignedIntToSignedByte(unsigned);//from-128to127...//Editingithereoutputstream.write(signedByteToUnsignedInt(signed));//from0-255看答案注意到创建自己的加密是不安全的,

c++ - 指向 deque<int>::push_back 的指针

#include#includeusingnamespacestd;main(){typedefvoid(deque::*func_ptr)(int);func_ptrfptr=&deque::push_back;}我试图获取指向该函数的指针,但出现编译错误error:cannotconvert‘void(std::deque::*)(constvalue_type&){akavoid(std::deque::*)(constint&)}’to‘func_ptr{akavoid(std::deque::*)(int)}’ininitializationfunc_ptrfptr=&deq

C++0x 用基于范围的 for 循环替换 for(int i;;) 范围循环的方法

所以我一直在使用GCC4.6进入新的C++,它现在具有基于范围的for循环。我发现这非常适合迭代数组和vector。主要出于审美原因,我想知道是否有办法用它来代替标准for(inti=min;i用类似的东西for(int&i:std::range(min,max)){}新的C++标准中是否有内置的东西允许我这样做?还是我必须编写自己的范围/迭代器类? 最佳答案 我在任何地方都看不到它。但这将是相当微不足道的:classrange_iterator:publicstd::input_iterator{intx;public:range

通过模板对unsigned int的C++限制

我正在使用一个模板将整数类型转换为二进制值的字符串表示形式。我使用了以下内容:templatestd::stringToBinary(constT&value){conststd::bitset::digits+1>bs(value);conststd::strings(bs.to_string());returns;}它适用于int但不能用unsignedint编译:unsignedintbuffer_u[10];intbuffer_i[10];...ToBinary(buffer_i[1]);//compileandworksToBinary(buffer_u[1]);//does

c++ - 将一行 cv::Mat 转换为 int

我有一个来自FREAK描述提取的描述符矩阵,其中每一行都是一个包含64个元素的描述符。我需要创建一个vector由于系统要求,从这个矩阵。到目前为止我试过这个:Mat_descriptors;std::vectordescriptors;introw;for(inti=0;i这是正确的还是有更好的方法? 最佳答案 descriptors中的所有值将指向带有此代码的堆栈上的变量row。看一个opencvMat的定义,row按值返回://returnsanewmatrixheaderforthespecifiedrowMatrow(in

c++ - 为什么 `constexpr const int &a = 1;` 在 block 范围内失败?

N45277.1.5[dcl.constexpr]p9Aconstexprspecifierusedinanobjectdeclarationdeclarestheobjectasconst.Suchanobjectshallhaveliteraltypeandshallbeinitialized.Ifitisinitializedbyaconstructorcall,thatcallshallbeaconstantexpression(5.20).Otherwise,orifaconstexprspecifierisusedinareferencedeclaration,everyf

c++ - 强制 String to int 函数消耗整个字符串

给定一个应该代表数字的字符串,我想将它放入一个转换函数中,如果整个字符串没有转换,该函数将提供通知。对于输入:“12”:istringstream::operator>>输出12atoi输出12stoi输出12对于输入"1X"我想要一个失败响应,但我得到:istringstream::operator>>输出1atoi输出1stoi输出1对于输入"X2":istringstream::operator>>输出0并设置错误标志atoi输出0stoi抛出错误[LiveExample]有没有办法在输入"1X"时引发错误行为? 最佳答案 编

c++ - 模板参数可以同时是 int 和 unsigned long 吗?

我有以下代码:#includetemplateclassA{};templateclassB;templateclassB>{};intmain(){B>b;return0;}在这里,B被模板化为int而A被模板化为size_t,它是一个unsignedlong我正在使用的两个编译器。当我使用编译器1(当前编译器)时,一切都按照我期望的方式编译和工作。当使用编译器2(我们正在转向的编译器)时,我收到一个编译器错误,指出B没有采用unsignedlong的模板特化-它已解释3作为unsignedlong因为它需要是A的一个,但是对于B。修复很明显,-只需更改B也采用size_t(或更改A