草庐IT

c++ - 使用 std::enable_if 和可变参数基类

这是我正在尝试做的一个简化示例:#include#include#includetemplateclassfoo{public:templatetypenamestd::enable_if::value>::typebar(constU&t){std::coutclassbaz:publicfoo...{};intmain(){bazb;b.bar(1.0);}这给了我模棱两可的函数错误:error:requestformember'bar'isambiguousb.bar(1.0);note:candidatesare:templatetypenamestd::enable_if::

c++ - 使用 std::enable_if 有什么问题?

我有一个函数set_data,我必须针对它接收的不同类型以不同的方式实现它。例如,它试图根据输入类型实现两个重载。如果是基本的,非void和非nullptr_t,则在第一次实现时处理它。如果它是std::string或char缓冲区,则以第二种方式处理它。structfield{templatevoidset_data(TDataTypedata,size_tindex=0);};template::value&&std::is_same::value==false&&std::is_same::value==false>::type>voidfield::set_data(TData

c++ - if(null) 正在使用 clang++ 编译的特定计算机中执行

我有一个大型代码,我们在团队中使用了很长时间。但是它在我的机器上编译时出现了几个星期的问题。代码针对IntelAtomCPU交叉编译并在特定机器上运行。当它在我的计算机上编译时,与其他人不同,它会导致段错误。段错误来自不应执行的ifblock:Settings*s=&Global::getSettings();std::coutGlobal::getSettings()如下:...private:static__threadSettings*theSettings;public:staticSettings&getSettings(){return*theSettings;}...__

c++ - 带 if 条件的作用域锁

我想创建作用域锁,但我想要类似的东西:{if(lockRequired)boost::mutex::scoped_lock(Mutex);//Afterthislinewegooutofscope/*HereIalsowanttohaveMutex*/}如果条件为真,我想要锁定互斥锁,但在升级范围内。我知道我可以使用简单的.lock并在范围末尾使用.unlock但我有很多返回路径。我还可以在范围内创建一些SynchronizationGuard并且whed析构函数被称为unlockmutex但这不是干净的解决方案。一些建议?最好的问候。 最佳答案

c++ - 我们如何在 C++ 中实现 if -goto 循环?

关闭。这个问题是notreproducibleorwascausedbytypos.它目前不接受答案。这个问题是由于错别字或无法再重现的问题引起的。虽然类似的问题可能是on-topic在这里,这个问题的解决方式不太可能帮助future的读者。关闭7年前。Improvethisquestion我们在学校学过ifgoto循环。讲师给出的程序不起作用。不工作我的意思是它被编译了,但是当我执行它时,输出什么也没有:#includeusingnamespacestd;intmain(){inti=0;prev:i++;//prevlabelcout要实现的实际循环等同于此for循环:for(in

c++ - 为 std::unordered_map 中的 const std::reference_wrapper 重载 operator==

我不知道如何使用std::reference_wrapper将std::string引用获取到std::unordered_map中>。根据以下链接,我知道我需要重载operator==。Whycantemplateinstancesnotbededucedin`std::reference_wrapper`s?但是,我不知道如何编写operator==以使其采用conststd::reference_wrapper。如果包装器不是const,那将不是问题。使用char而不是std::string效果很好(不需要重载operator==)。代码:#include#include#inc

c++ - map 中过期的 weak_ptr 会发生什么

我想了解weak_ptr已过期的映射中的条目(类型为boost::weak_ptr)会发生什么。map中的相应条目是否会自动删除?键是一个整数,对应的值是一个weak_ptr。我写的示例代码,但无法编译#include#include#includeusingnamespacestd;classFoo:publicboost::enable_shared_from_this{public:Foo(intn=0):bar(n){std::coutinc_ref(){returnshared_from_this();}private:intbar;};std::map>mappy;intm

c++ - 使用 std::enable_if 保护复制构造函数

我编写了一个类来促进具有以下构造函数的类型删除:classEnvelope{public:Envelope(){}templateEnvelope(Runnablerunnable):m_runFunc(&Envelope::RunAndDeleteRunnable),m_runnable(newRunnable(runnable)){}templateEnvelope(Runnable*runnable):m_runFunc(&Envelope::RunRunnable),m_runnable(runnable){}};我想重写第一个非默认构造函数以获取引用而不是值(Runnable

c++ - 接收通用 map 作为参数的模板函数

在很多情况下,我们希望对完全相同的std::map或std::unordered_map执行一些操作,独立于map的类型。让我们考虑以下示例:#include#include#includetemplateclassContainer>voidprintMap(ContainerinputMap,booladditionalParam=false){for(constpairp:inputMap)coutmap1;map1.emplace(a,b);unordered_mapmap2;map2.emplace(a,b);printMap(map1);printMap(map2);ret

c++ - 大条目的慢 std::map

我们有48,16,703个这种格式的条目。1abc2def......4816702blah4816703blah_blah由于条目的数量很大,我担心std::map在插入期间会花费很多时间,因为它需要为每次插入做平衡。仅将这些条目插入map会花费大量时间。我在做map[first]=second;两个问题:1.我在这种情况下使用std::map是否正确?2.我按上面的方式插入是否正确。或者我应该使用map.insert()很抱歉没有做实验并写下绝对数字,但我们希望就我们做的事情是否正确达成普遍共识。此外,它们的键并不总是连续的..附言当然,稍后我们还需要访问该映射以获取与键对应的值。