草庐IT

c++ - std::type_index 跨 DLL 是否安全

假设我有一个主DLL,其中有一个这样的类:classTest{public:typedefstd::unordered_mapMap;templatevoidSetValue(intval){SetValue(std::type_index(typeid(T)),val);}templateintGetValue(){returnGetValue(std::type_index(typeid(T)));}protected://Definedin.cppfilevoidSetValue(conststd::type_index&idx,intval){m_Map[idx]=val;}/

c++ - std::unique_ptr 使用带有少量参数的自定义删除器

我想知道是否可以使用多个参数(标准删除器签名)为std::unique_ptr指定自定义删除器。我知道std::shared_ptr存在std::bind的解决方法,这使得它成为可能但是std::unique_ptr存在一些技巧吗?对我来说似乎不是因为根据http://en.cppreference.com/w/cpp/memory/unique_ptr:Typerequirements-DeletermustbeFunctionObjectorlvaluereferencetoaFunctionObjectorlvaluereferencetofunction,callablewit

c++ - std::map 通过变换替换现有元素

我有一个代码:std::vectorvector={1,3,5,7,9};usingmy_type=std::pair;std::map>map;for(constauto&i:vector){map[i]=boost::none;}constmy_typeval={1,5};std::transform(vector.cbegin(),vector.cend(),std::inserter(map,map.end()),[&val](constint&i){returnstd::make_pair(i,boost::optional(val));});一切正常,但std::trans

c++ - 为什么在抛出 'std::bad_alloc' 实例后调用终止?

每1秒,函数工作一次。我的系统是linux。奔跑突然死亡。-----global-------staticintarrayNum[33000];-------------------function(){unsignedshortint**US_INT;US_INT=newunsignedshortint*[255];for(inti=0;i程序停止。和留言↓在抛出“std::bad_alloc”的实例后终止调用what():std::bad_alloc 最佳答案 bad_alloc异常是由内存分配失败触发的(因此您的new之一)。

c++ - 将 std::bitset 分成两半?

我正在实现DES算法,我需要拆分std::bitsetpermutationKey分成两半。std::bitsetpermutationKey(0x133457799BBCDF);std::bitsetleftKey;std::bitsetrightKey;std::bitsetdivider(0b00000000000000000000000000001111111111111111111111111111);rightKey=permutationKey÷r;leftKey=(permutationKey>>28)÷r;我试着打字bitset至bitset但

c++ - 将 initializer_list 插入 std::vector 时出现 "Invalid iterator range"

此代码在Ideone上按预期编译并运行良好:#include#include#includeintmain(){std::vectorstrVec;strVec.insert(strVec.end(),{L"black",L"white",L"red"});strVec.insert(strVec.end(),{L"blue",L"green"});//STLexceptionfor(auto&i:strVec){std::wcout但是,在MSVC(VisualStudio2013)中因“无效的迭代器范围”而失败。有什么见解吗?顺便说一句,插入更多元素是可行的,例如在第二个插入中,这

c++ - Valgrind 下 Mac OS 上的 std::thread.join() SIGSEGV

以下简单代码(C++11)将仅在MacOS和Linux上运行:#include#include#includevoidthreadFunction(){for(intcc=0;cc但是,在Valgrind中包装执行:valgrind--leak-check=full--show-reachable=no--track-fds=yes--error-exitcode=1--track-origins=yes./theexecutable...它现在可以在Linux上运行,但不能在MacOSX上运行,失败:==47544==Processterminatingwithdefaultacti

c++ - 为什么我需要在临时 dynamic_bitset 上调用 std::move?

我在这里讲一个冗长的背景故事,因为除了直接回答之外,我还想知道我导致这种情况的推理是否正确。我有一个接受dynamic_bitset的函数参数(来自Boost.dynamic_bitset)。说它看起来像这样。voidfoo(boost::dynamic_bitsetdb){//dostuff}碰巧它只被临时调用,从构造函数构建,如foo(boost::dynamic_bitset{5}.set())(使用5位位集调用所有位集)。我的位集只有少量的位(少于32)。所以起初,我想“我只是按值传递它;拷贝比指针小。”但后来我想“它是动态的,所以它必须在堆上分配空间。我想避免不必要的分配和释

c++ - 在 std::unordered_map 中使用模板化键

我不明白为什么我的编译器不接受下面的代码#include#includetemplateusingM=std::unordered_set;templateusingD=M;templateusingDM=std::unordered_map::const_iterator//Problem,typenameD::const_iterator>;//Problemintmain(intargc,char**argv){Dd;Mm;DMdm;//Problem}编译命令是clang++-std=c++14test.cpp-otest编译器错误消息摘录是/usr/bin/../lib/gc

c++ - 错误 : 'Failed to specialize function template' C2893 'std::invoke'

我正在VisualStudio2013中编写MFC程序,但我不断收到以下两个错误错误C2893无法特化函数模板'unknown-typestd::invoke(_Callable&&,_Types&&...)'和错误C2672“std::invoke”:找不到匹配的重载函数错误与文件xthread第238行有关我是c++/MFC的新手,我正在尝试编写一个将在后台运行到系统时间的函数。这是我使用的代码:voidtask1(ExperimentTab&dlg){while(true){CStringshowtime=CTime::GetCurrentTime().Format("%H:%M