草庐IT

T_Unsigned

全部标签

C++ 模板比 int->unsigned 转换更好?

我有如下两个函数templateunsignedintmyFunction(Tmyelement){myelement->func();return2;}voidmyFunction(unsignedintmyelement){}我正在使用以下代码myFunction(2);visualstudio2012提示“int没有->func()”。为什么不使用unsignedint版本? 最佳答案 您误读了错误消息。编译器不使用该函数,而是实例化它以确定它是否是候选函数。您需要禁用不适合类型的实例化:templatetypenamestd

c++ - 转换 int -> unsigned long long 是否由标准定义

我无法在标准中找到关于如何将int值转换为unsignedlonglong的确切规范。指定了各种类似的转换,如int->unsigned、unsigned->int(UBifnegative)、unsignedlonglong->int等例如GCC,-1被转换为0xffffffffffffffff,而不是0x00000000ffffffff。我可以依赖这种行为吗? 最佳答案 是的,这个定义很明确,它基本上是将maxunsignedlonglong+1添加到-1,它始终是maxunsignedlonglong。这包含在draftC++

c++ - 从 unsigned long long 转换为 float with round 到最接近的偶数

我需要编写一个函数,将unsignedlonglong舍入为float,并且舍入应朝向最近的偶数。我不能只进行C++类型转换,因为AFAIK标准没有指定舍入。我正在考虑使用boost::numeric,但在阅读文档后我找不到任何有用的线索。这可以使用那个库来完成吗?当然,如果有替代方案,我会很乐意使用它。如有任何帮助,我们将不胜感激。编辑:添加示例以使事情更清楚一些。假设我想将0xffffff7fffffffff转换为其浮点表示形式。C++标准允许以下任一项:0x5f7fffff~1.9999999*2^630x5f800000=2^64现在,如果你加上最接近偶数的舍入限制,只有第一个

c++ - 将 unsigned long long 与 signed long long 结果相减?

假设我有这两种类型:typedefunsignedlonglonguint64;typedefsignedlonglongsint64;我有这些变量:uint64a=...;uint64b=...;sint64c;我想从a中减去b并将结果赋给c,很明显,如果差值的绝对值大于2^63,那么它将换行(或未定义),这是可以的。但是对于绝对差小于2^63的情况,我希望结果是正确的。以下三种方式:c=a-b;//signconversionwarningignoredc=sint64(a-b);c=sint64(a)-sint64(b);其中哪些可以保证按标准工作?(以及为什么/如何?)

c# - 如何将带有 unsigned char* 的结构从 C# 传递到 C++?

我有一些带有结构描述和一些方法的C++dll:structMSG_STRUCT{unsignedlongdataSize;unsignedchar*data;}和功能例如:unsignedlongReadMsg(unsignedlongmsgId,MSG_STRUCT*readMsg){readMsg->dataSize=someDataSize;readMsg->data=someData;}所以我想从C#调用这个函数:[StructLayout(LayoutKind.Sequential)]structMSG_STRUCT{UInt32dataSize;byte[]data;}[D

c++ - 将 'const wchar_t *' 转换为 'unsigned char *'

在C++中是否可以将'constwchar_t*'转换为'unsignedchar*'?我该怎么做?wstringdirName;unsignedchar*dirNameA=(unsignedchar*)dirName.c_str();//Iamcreatingahashfromastringhmac_sha256_init(hash,(unsignedchar*)dirName.c_str(),(dirName.length)+1); 最佳答案 你需要一个字符一个字符地转换。有像wcstombs这样的函数可以做到这一点。

c++ - unsigned char 和 char 指针之间的区别

我对unsignedchar(在WinAPI中也是BYTE)和char指针之间的差异感到有点困惑。目前我正在处理一些基于ATL的遗留代码,我看到了很多如下所示的表达式:CAtlArrayrawContent;CALL_THE_FUNCTION_WHICH_FILLS_RAW_CONTENT(rawContent);returnArrayToUnicodeString(rawContent);//orreturnArrayToAnsiString(rawContent);现在,ArrayToXXString的实现如下所示:CStringAArrayToAnsiString(constCA

c++ - 推导非类型模板参数 Unsigned Int/size_t

我正在尝试推断一个非类型模板参数。#includetemplatevoidgetsize(unsignedints){std::cout(4);//orevengetsize();//Isthispossible?}但是我得到了错误:deduce_szie_t.cpp:Infunction'intmain()':deduce_szie_t.cpp:9:15:error:nomatchingfunctionforcallto'getsize(unsignedint)'deduce_szie_t.cpp:9:15:note:candidateis:deduce_szie_t.cpp:4:6

c++ - 为什么 "unsigned int ui = {-1};"是缩小转换错误?

标准§8.5.4/7解释了什么是缩小转换:Anarrowingconversionisanimplicitconversion—fromafloating-pointtypetoanintegertype,or—fromlongdoubletodoubleorfloat,orfromdoubletofloat,exceptwherethesourceisaconstantexpressionandtheactualvalueafterconversioniswithintherangeofvaluesthatcanberepresented(evenifitcannotberepres

c++ - 将 std::shared_ptr<char> 转换为 std::shared_ptr<unsigned char>

有什么好的方法可以将shared_ptr转换为shared_ptr吗?我想到了以下但它看起来不是很干净。intmain(intargc,char**argv){std::shared_ptrp1=std::make_shared();std::shared_ptrp2=std::shared_ptr(reinterpret_cast(p1.get()),[p1](unsignedchar*){});} 最佳答案 你正在做的事情有一个现成的功能,reinterpret_pointer_cast:std::shared_ptrp2=st