草庐IT

atomic_long_try_cmpxchg_acquire

全部标签

将 double 添加到 long long 时出现 C++ 精度错误

这个问题在这里已经有了答案:ImplicittypeconversionrulesinC++operators(9个回答)关闭4年前。我注意到,在将double添加到longlong时,VisualStudio会出现精度错误。例如:longlonga=44981600439878676;doubleb=234567890;a+=b;a的结果是44981600674446560,但应该是44981600674446566。它发生在x32和x64上。但是以下返回正确的值:longlonga=44981600439878676;doubleb=234567890;a+=(longlong)b

c++ - 为什么 `uint64_t` 的模板特化与 Mac 平台上的 `unsigned long` 不匹配?

这个问题在这里已经有了答案:longlongintvs.longintvs.int64_tinC++(3个答案)关闭3年前。为什么以下代码片段在Mac平台上使用clang++编译失败?sizeofunsignedlong和uint64_t都是8,所以我认为它们是同一类型。那么为什么编译认为Serializer是抽象的吗?因为我定义了Serializer,Serializer,Serializer,Serializer,Serializer,Serializer,Serializer,Serializer,有没有办法解决这个问题并避免定义更多类型,如Serializer?错误信息如下,c

c++ - 从二进制文件读取字节到 long int

我有两个问题:我有一个二进制文件中的数据。我想通过使用读取函数读取前8个字节以签名longint,但我不能。你知道我该怎么做吗?如何直接读取一段数据为字符串?我可以像中所示那样阅读吗例如:ifstreamis;is.open("test.txt",ios::binary);stringstr;is.read(str.c_str,40);//40bytesshouldberead 最佳答案 Iwantreadfirst8bytestosignedlongintbyusingreadfunctionbutIcouldnot.Doyouk

c++ - 为什么我收到错误 : initializing argument 1 of 'Item::Item(int)' [-fpermissive] in Eclipse when I try to compile my C++ code?

我是C++的新手,在盯着它看了太久之后终于放弃了尝试编译它。编译器似乎出于某种原因拒绝了头文件中的构造函数原型(prototype)......我无法弄清楚它有什么问题。项目.h:#ifndefITEM_H_#defineITEM_H_classItem{public:Item(int);//ThislineiswhatEclipsekeepsflaggingupwiththeerrorinthetitlevirtual~Item();Item*getNextPtr();intgetValue();voidsetNextPtr(Item*);};#endif/*ITEM_H_*/在我的

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

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

c++ - 在构造函数 C++ 中处理 Try-catch block

我面临这样一种情况,我需要在构造函数中使用try-catchblock。特别是,构造函数尝试调用tryblock中的函数,如果失败,它将调用另一个函数来设置一些值。在这两种情况下,构造函数都应该正确地创建对象,并且在这两种情况下它都必须在结束之前调用一些其他方法。情况如下:classA{A(inti){try{setDevice(i);}catch(DeviceException&ex){setDevice(0);throwex;}otherMethod();}}但是,如果在tryblock中发生错误,则不会调用otherMethod(),因为执行在catchblock内结束,我不能像

c++ - C++ 如何处理 try catch block 中的赋值?

我使用clang分析器检查我的C++代码是否存在缺陷和错误。我有以下构造:#include#includedoublesomethingThatMayThrow()throw(std::exception){if(rand()%2){throwstd::exception();}return5.0;}intmain(){doublevalue=2.0;try{value=somethingThatMayThrow();}catch(conststd::exception&){std::cout分析器现在提示变量value的初始值从未被读取。但是,很明显,当且仅当tryblock中出现异

c++ - boost::phoenix try_catch_all 构造编译失败

我正在编写用于日期解析的boost::spirit::qi语法。#include#include#includetemplatestructdate_rfc1123_grammar:boost::spirit::qi::grammar{typedefboost::gregorian::datevalue_type;date_rfc1123_grammar():date_rfc1123_grammar::base_type(date){namespaceqi=boost::spirit::qi;namespacephx=boost::phoenix;usingqi::_pass;usin

c++ - 如何正确增加 C++11 std::atomic?

我是多线程编程的新手,我发现了C++11中的std::atomic。所以,我试图弄清楚原子操作需要多少时间。我试过这段代码:usingnamespacestd;usingnamespacestd::chrono;constexprintNUM_THREADS=8;constexprintLIMIT=100000;atomicsum=0;voidfoo(intidx){while(true){if(sum.load()>=LIMIT){return;}sum.fetch_add(1);}}与主要:intmain(void){threadthreads[NUM_THREADS];autos

c++ - std::atomic 用于内置类型 - 非无锁与琐碎的析构函数?

查看std::atomic这是我阅读的默认专业:Thesespecializationshavestandardlayout,trivialdefaultconstructors,andtrivialdestructors.我还阅读了is_lock_free:Allatomictypesexceptforstd::atomic_flagmaybeimplementedusingmutexesorotherlockingoperations,ratherthanusingthelock-freeatomicCPUinstructions.Atomictypesarealsoallowed