草庐IT

c++ - if 子句中的自动赋值

我有下面的代码,它报告“这里不允许自动”//GeSettings()returnsboost::optionalandcouldbeemptyif((autoret=GetSettings(InputField))&&ShouldWeDoThis()){dosomethingwithret;}但如果没问题,可以如下更改。if(autoret=GetSettings(InputField)){if(ShouldWeDoThis()){dosomethingwithret;}}背后的原因可能是傻买请问为什么?我正在使用VisualStudio2017 最佳答案

c++ - 在循环/if 括号后检测分号

GCC中是否有任何标志(如clang中的-Wempty-body),可以帮助我检测while/for循环大括号后的分号?有时人类很难发现这些简单的错误。inti=0;for(i=0;i我使用GCC4.7.3和clang3.2-1~exp9ubuntu1。编辑:我还检查编译器是否可以帮助我在“if-else语句”之后找到这些错误。if(i==0){cout有趣的是gcc通过打印警告比clang更有帮助(带有此标志(-Wall-pedantic-Wempty-body):main.cpp:30:9:warning:suggestbracesaroundemptybodyinan‘else’

c# - 与 COM 中的 QueryInterface 或 C++ 中的 dynamic_cast 相比, "as"的成本是多少?

我仍在尝试将我深厚的旧知识从C/C++映射到我较浅的.Net知识。今天是时候在C#中使用“as”(隐含地“is”和cast)了。我对“as”的心理模型是,它是一个QueryInterface或dynamic_cast(一个带有指针参数的dynamic_cast,而不是引用,也就是说)对于C#。我的问题有两个方面:我的比较公平吗?与QueryInterface或dynamic_cast相比,“as”的相对成本是多少? 最佳答案 是的,比较是公平的,尤其是在处理指针时。这三个中的每一个要么成功并返回目标类型的非空指针,要么返回null。

c++ - GMock : How to return mock class variable as the return value

我是第一次尝试使用GMock(用于C++的谷歌模拟框架)。我有以下类(class):classLocalCache{public:virtualtime_tGetCurrentTime()=0;virtualintAddEntry(conststd::stringkey,std::string&value);virtualintGetEntry(conststd::stringkey,std::string&value);};GetEntry方法调用GetCurrentTime调用。我想模拟GetCurrentTime方法,以便我可以在测试中提前时钟以测试作为GetEntry调用的一部

c++ - SFINAE : Detecting if a function is called by a compile time known value

我喜欢在我的一个ctors以编译时已知值被调用时做一些检查。有办法检测吗?所以当有人调用它时:Aa(10);因为10是编译时已知常量,所以我喜欢调用一个特殊的构造函数,如下所示:template>A(intValue){}知道如何解决这个问题吗?谢谢! 最佳答案 积分常量可以解决您的问题:structA{template*=nullptr>A(std::integral_constant){}};然后,你可以像这样使用它:Aa{std:integral_constant{}};为了便于使用,您还可以使用类似于boost::hana的

c++ - std::forward_list::remove_if 谓词的要求

考虑这段代码:structT{boolstatus;UsefulDatadata;};std::forward_listlst;lst.remove_if([](T&x)->bool{returnx.status=!x.status;});即一次性切换状态和删除非事件元素。根据cppreference上面的代码似乎是未定义的行为(强调我的):templatevoidremove_if(UnaryPredicatep);p-unarypredicatewhichreturnstrueiftheelementshouldberemoved.Thesignatureofthepredicat

c++ - MSVC 无法评估 enable_if 中的 constexpr 函数

考虑一个简单的效用函数来计算合取,并使用这个效用来确保std::tuple中的类型都相等。#include#includeconstexprautoall()noexcept->bool{returntrue;}templateconstexprautoall(boolconstx,Bools...xs)noexcept->bool{returnx&&all(xs...);}templatestructfoo;templatestructfoo,std::enable_if_t::value...)>>{};intmain(){foo>x;}GCC和Clang可以处理这段代码,但MSV

c++ - boost::enable_if_c 似乎不起作用

这个问题在这里已经有了答案:关闭10年前。PossibleDuplicate:Howtouseenable_iftoenablememberfunctionsbasedontemplateparameterofclass我有一个类模板:templateclassVector我想为特定的N启用构造函数,所以我这样做:Vector(typenameboost::enable_if_c::typeconst&e0,Tconst&e1){data[0]=e0;data[1]=e1;}但是编译器(MSVC2010SP1)给我一个错误,而不是应用SFINAE。错误是:errorC2039:'typ

c++ - unordered_set::remove_if(): C3892: 无法分配给常量变量

我有这样的代码:unordered_setoutput;...autorequiredType=variables.at(arg.value);autoend=remove_if(output.begin(),output.end(),[&](AttrValuex){return!matchingOutputType(requiredType,ast->getNodeType(ast->getNodeKeyAttribute(x)));});//queryevaluator_getcandidatelist.cpp(179)output.erase(end);错误在代码的第4行。所以我

c++ - 如何正确使用enable_if?

我需要学习如何使用enable_if。为此,我需要使用enable_if重新实现距离函数。我试过这个:#include#include#include#include#includetemplatetypenamestd::enable_if::value,std::iterator_traits::difference_type>::typemy_distance(Inbegin,Inend,std::input_iterator_tagdummy){typenamestd::iterator_traits::difference_typen=0;while(begin!=end){