草庐IT

c++ - 警告 C4244 : 'argument' : conversion from 'double' to 'const int' , 可能丢失数据

我正在定义“*”运算符以使用“NumericArray”类模板。代码如下:templateNumericArrayNumericArray::operator*(constT&factor)const{NumericArraynewArray(Size());for(inti=0;i当我尝试将类型为“int”的“NumericArray”(NumericArray)与“*”运算符一起使用时,当“factor”参数为double时:intArray1=intArray1*2.5;我收到以下编译器警告:warningC4244:'argument':conversionfrom'doubl

c++ - 使用 swig typemap 将 vector<pair<int,int>> & 从 c++ 方法返回到元组的 python 列表

我在尝试使用%typemap(out)包装一个将对vector对的常量引用返回到Python元组列表的C++方法时遇到了很多麻烦。我目前有这样的东西:我的类.h:#inlcudeusingstd::vector;classMyClass{private:constvector>&_myvector;public:MyClass(constvector>&myvector);constvector>&GetMyVector()const;}我的类.cpp:#include"myclass.h"MyClass::MyClass(constvector>&myvector):_myvecto

c++ - 错误 : invalid operands of types 'int' and '<unresolved overloaded function type>' to binary 'operator<<'

我想获取z_Data的第48个字符的第6位{charc=pPkt->z_Data[47];//thisz_Dataisacharbufferstd::cout>3)&1>4)&1>5)&1 最佳答案 优先级高于&,所以你需要:std::cout>3)&1)>4)&1)>5)&1) 关于c++-错误:invalidoperandsoftypes'int'and''tobinary'operator https://stackoverflow.com/questions/246

c++ - 如何正确地将 time_t 转换为 long int?

我仍在学习C++中的类型转换,目前我正在做这件事longintt=time(NULL);我正在使用VS2013并注意到从“time_t”到“long”警告的转换,所以我想我应该将其强制转换为看起来像;longintt=static_casttime(NULL);然而,这还行不通,但结合静态转换和C风格转换仍然有效longintt=static_cast(time(NULL));我只是想知道是否有人可以帮助阐明这一点? 最佳答案 time(NULL)不是强制转换而是返回time_t的函数调用.自time_t与longint不完全相同的

c++ - unsigned int 到 unsigned long long 定义明确吗?

我想看看当unsignedlonglong被赋值给unsignedint时幕后发生了什么。我制作了一个简单的C++程序来试用它,并将所有io移出main():#include#includevoidusage(){std::cout\n";exit(0);}voidatoiWarning(intfoo){std::cout生成的程序集为main生成了这个:0000000000400950:400950:55push%rbp400951:4889e5mov%rsp,%rbp400954:4883ec20sub$0x20,%rsp400958:897decmov%edi,-0x14(%rb

c++ - 散列任意精度值(boost::multiprecision::cpp_int)

我需要以任意精度获取一个值的散列值(来自Boost.Multiprecision);我用cpp_int后端。我想出了以下代码:boost::multiprecision::cpp_intx0=1;constautoseed=std::hash{}(x0.str());我不需要代码尽可能快,但我发现对字符串表示进行哈希处理非常笨拙。所以我的问题是双重的:保持任意精度,我可以更有效地散列值吗?也许我不应该坚持保持任意精度,我应该转换成一个我可以轻松散列的double(不过,我仍然会使用任意精度值进行哈希表所需的比较)? 最佳答案 您可以

c++ - 为什么采用 int 的函数重载优于采用 unsigned char 的函数重载?

考虑这个程序:#includeusingnamespacestd;voidf(unsignedcharc){cout这会打印出97,表明选择的f()重载是采用int的重载。我觉得这很奇怪;直觉上unsignedchar不是更适合char吗? 最佳答案 wouldn'tintuitivelyanunsignedcharbeabettermatchforachar?嗯,我想,但不是根据标准。根据[conv.prom]p1:Aprvalueofanintegertypeotherthanbool,char16_­t,char32_­t,o

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++ - int() 和 int(*)() 有什么区别?

我正在尝试创建一个识别函数的类模板,我可以在其中识别函数何时专门化R(*)()的类模型,但在std::function你可以申报return_type(),和std::is_same.::value为零。这是什么int()statementmean和int()有什么区别和int(*)()?更新:所以int()是函数声明或函数类型并且int(*)()是指向函数的指针。但是waht是int(std::string::)()的类型函数?类似于intstd::string::(),或喜欢std::functionint(conststd::string&)?如何让这个程序输出1?#includ

c++ - 不同类型的 `decltype((const int)a)` 和 `decltype((const int)1)`

//g++7.3templatestructtd;intmain(){inta=1;tdt1;tdt2;return0;}编译结果如下:错误:聚合‘tdt1’类型不完整,无法定义错误:聚合‘tdt2’类型不完整,无法定义那么,为什么decltype((constint)a)的类型是和decltype((constint)1)不一样? 最佳答案 说明符decltype((constint)a)和decltype((constint)1)都解析为int。这是因为没有const非类类型的纯右值,如C++17[expr]中所述:Ifaprv