草庐IT

c++ - 我可以在多线程 C++ 中安全地使用 int 吗?

我有以下代码。有什么可以使它成为非线程安全的吗?classrunner{public:volatileintexitFlag;//constructinthreadArunner(){exitFlag=0;}//runitinthreadBvoidthreadFunc(){//doesnotmatterwhenthechangeisgettingherewhile(exitFlag==0){//...dostuff(notusingexitFlag)}}//callitfromthreadAvoidsignalThread(){exitFlag=1;//onlychangeonebit

c++ - BOOST_TYPEOF 返回 int 而不是 const int

你能解释一下为什么打印1吗?BOOST_TYPEOF不应返回constint。如何在不使用C++11功能的情况下检查函数是否返回const?#include#include#includeconstintf_const_int(){return1;}intmain(){typedefBOOST_TYPEOF(f_const_int())type;std::cout::value) 最佳答案 如果纯右值表达式的类型为cvint,则忽略该cv限定符。[表达式]/6:Ifaprvalueinitiallyhasthetype“cvT,”w

c++ - int64_t 再次加倍到 int64_t,精度损失

我需要解析用科学记数法表示的给定类型(例如:longlonginteger)。示例:123456789012345678.3e-3123456789012345678.3我知道给定字符串的类型,但我不能使用strtoll,因为数字是以科学记数法给出的。我所做的是使用strtod转换它,对int64_t进行错误检查并将其转换回int64_t。ErrCheckInt和ErrCheckDouble对整数和浮点类型进行错误检查(上溢、下溢等),并将数字转换为任何类型。.doubleres=strtod(processedStr,&end);return(std::is_floating_poi

c++ - 错误 : expected primary-expression before ‘int’

我正在使用:gcc--版本gcc(Ubuntu4.9.2-0ubuntu1~14.04)4.9.2我正在尝试编译以下程序:#include#includeusingnamespacestd;intmain(){cout但是得到如下错误:g++-fcilkplusCilk_1.cppCilk_1.cpp:Infunction‘intmain()’:Cilk_1.cpp:9:12:error:expectedprimary-expressionbefore‘int’cilk_for(inti=0;i怎么了?谢谢 最佳答案 来自linkC

c++ - `reinterpret_cast<char*>(reinterpret_cast<uintptr_t>(&ch) + 1) == &ch +1` 有保证吗?

我正在编写与对齐相关的代码,如果给定的指针正确对齐,却没有标准的功能测试,这让我感到非常惊讶。似乎互联网上的大多数代码都使用(long)ptr或reinterpret_cast(ptr)测试对齐,我也使用了它们,但我想知道使用指向整数类型的强制转换指针是否符合标准。这里是否有任何系统触发断言?charch[2];assert(reinterpret_cast(reinterpret_cast(&ch[0])+1)==&ch[1]); 最佳答案 回答题目中的问题:没有。反例:在旧的Pr1me微型计算机上,一个普通指针是两个16位字。第

c++ - 通过强制转换为 int 来访问原始内存是否违反了严格的别名?

假设我想为int动态分配空间,并将最大可表示值写入该内存。想到这段代码:autorawMem=std::malloc(sizeof(int));//rawMem'stypeisvoid**(reinterpret_cast(rawMem))=INT_MAX;//INT_MAXfrom此代码是否违反了C++关于strictaliasing的规则??g++和clang++都不会提示-Wall-pedantic。如果代码不违反严格的别名,为什么不呢?std::malloc返回void*,所以虽然我不知道std::malloc返回的内存的静态和动态类型是什么>是,没有理由认为两者都是int。而

c++ - 为什么 float 和 int 的 std::pow 会调用此重载?

以下内容:#includeintmain(){floatbase=2.0f;floatresult=std::pow(base,2);return0;}在打开时触发-Wconversion警告。Wandbox似乎调用了std::pow的double重载,我希望选择float重载(与int指数被转换为float)。知道他重载的人能解释一下为什么吗? 最佳答案 自C++11起,混合参数pow将任何整数参数强制转换为double。混合参数函数的返回类型始终是double除非一个参数是longdouble然后结果是longdouble。[c

C++ 在 ‘value_type’ 中没有名为 ‘struct std::iterator_traits<int>' 的类型

你好。我正在尝试运行以下代码(仅用于培训目的):#include#includetemplate>classkont>typenamestd::iterator_traits::value_typefoo_test(typenamekont::iteratorb){return*b;}templatetypenamestd::iterator_traits::value_typeminimum(Iterb,Itere){Iterm=b;/*CODE*/return*m;}intmain(void){std::listx;x.push_back(10);x.push_back(100);

c++ - std::unordered_map<int,float> 是否仍然需要散列整数才能得到值?

我想知道是否std::unordered_map仍然必须对给定的整数进行哈希处理才能得到该值,或者直接使用它。我需要每秒多次快速执行此操作,如std::hash不能保证是身份函数,我将如何重新定义它?(显然不使用STL并编写我自己的容器是可能的,但我怀疑我编写的容器是否会更有效率(可能慢得多,慢得多))。谢谢! 最佳答案 Iwouldliketoknowwhetherstd::unordered_mapstillhastohashthegiveninteger是的。Ineedtoperformthisoperationveryfas

c++ - 用双花括号初始化 vector :std::string vs int

在这个问题的回答中:Initializingvectorwithdoublecurlybraces表明vectorv={{"a","b"}};将调用带有initializer_list的std::vector构造函数和一个元素。因此vector中的第一个(也是唯一一个)元素将从{"a","b"}构造。这会导致未定义的行为,但这超出了这里的重点。我发现的是std::vectorv={{2,3}};将使用两个元素的initializer_list调用std::vector构造函数。造成这种行为差异的原因是什么? 最佳答案 类类型列表初始