草庐IT

numerical_value

全部标签

c++ - 为什么 std::numeric_limits<seconds>::max() 返回 0?

我在std::numeric_limits::max()中发现了一个有趣的问题。返回0。答案是使用seconds::max()或std::numeric_limits::max()相反,但我很想知道为什么会发生这种情况。我希望它在编译时失败或正常工作。以下代码演示了gcc4.9.3的问题。#include#include#includeusingnamespacestd;usingnamespacestd::chrono;intmain(int/*argc*/,constchar*/*argv*/[]){constautomaxSeconds=std::numeric_limits::

c++警告: enumeration value not handled in switch [-Wswitch]

我正在尝试编译以下代码而没有警告:while(window.pollEvent(event)){switch(event.type){casesf::Event::Closed:window.close();break;casesf::Event::KeyPressed:if(event.key.code==sf::Keyboard::Escape)window.close();if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space))particleSystem.fuel(200/**window.getFrameTime()*/);if(

c++警告: enumeration value not handled in switch [-Wswitch]

我正在尝试编译以下代码而没有警告:while(window.pollEvent(event)){switch(event.type){casesf::Event::Closed:window.close();break;casesf::Event::KeyPressed:if(event.key.code==sf::Keyboard::Escape)window.close();if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space))particleSystem.fuel(200/**window.getFrameTime()*/);if(

c++ - 为什么 back_insert_iterator/front_insert_iterator/insert_iterator 的 value_type/difference_type/pointer/reference 都是 void?

在我的项目中,我想将流拆分为一些给定类型的值,所以我实现了一个模板函数templateTOutputIterSplitSpace(std::istream&IS,TOutputIterresult){TElemelem;while(IS>>elem){*result=elem;++result;}returnresult;}我认为这很尴尬,因为我必须在调用时明确给出TElem的类型。例如,我必须写:std::vectorv;SplitSpace(std::cin,back_inserter(v));//IwanttoittobeSplitSpace(std::cin,back_inse

c++ - 为什么 back_insert_iterator/front_insert_iterator/insert_iterator 的 value_type/difference_type/pointer/reference 都是 void?

在我的项目中,我想将流拆分为一些给定类型的值,所以我实现了一个模板函数templateTOutputIterSplitSpace(std::istream&IS,TOutputIterresult){TElemelem;while(IS>>elem){*result=elem;++result;}returnresult;}我认为这很尴尬,因为我必须在调用时明确给出TElem的类型。例如,我必须写:std::vectorv;SplitSpace(std::cin,back_inserter(v));//IwanttoittobeSplitSpace(std::cin,back_inse

c++ - "Swapping values of two variables without using a third variable"中的潜在问题

我最近发现了这种方法,可以在不使用第三个变量的情况下交换两个变量的值。a^=b^=a^=b但是当我在不同的编译器上尝试上面的代码时,我得到了不同的结果,有些给出了正确的结果,有些没有。代码有什么严重错误吗? 最佳答案 Isanythingterriblywrongwiththecode?是的!a^=b^=a^=b实际上调用C和C++中的未定义行为,因为您试图更改a的值在两个序列点之间不止一次。尝试写作(虽然不是万无一失)a^=b;b^=a;a^=b;而不是a^=b^=a^=b.P.S:切勿尝试在不使用第三个变量的情况下交换两个变量的

c++ - "Swapping values of two variables without using a third variable"中的潜在问题

我最近发现了这种方法,可以在不使用第三个变量的情况下交换两个变量的值。a^=b^=a^=b但是当我在不同的编译器上尝试上面的代码时,我得到了不同的结果,有些给出了正确的结果,有些没有。代码有什么严重错误吗? 最佳答案 Isanythingterriblywrongwiththecode?是的!a^=b^=a^=b实际上调用C和C++中的未定义行为,因为您试图更改a的值在两个序列点之间不止一次。尝试写作(虽然不是万无一失)a^=b;b^=a;a^=b;而不是a^=b^=a^=b.P.S:切勿尝试在不使用第三个变量的情况下交换两个变量的

c++ - 警告 C4800 : 'BOOL' : forcing value to bool 'true' or 'false' (performance warning)

当我在VisualStudio2008中编译以下代码片段时,我收到了这个警告。BOOLCPlan::getStandardPlan()const{returnm_standardPlan;}boolm_bStandardPlan;if(plan!=NULL){//AssignthevaluestotheColaobjectpoCola->m_lPlanId=plan->getPlanId();poCola->m_lPlanElementId=plan->getPlanElementId();poCola->m_lPlanElementBaseId=plan->getPlanElemen

c++ - 警告 C4800 : 'BOOL' : forcing value to bool 'true' or 'false' (performance warning)

当我在VisualStudio2008中编译以下代码片段时,我收到了这个警告。BOOLCPlan::getStandardPlan()const{returnm_standardPlan;}boolm_bStandardPlan;if(plan!=NULL){//AssignthevaluestotheColaobjectpoCola->m_lPlanId=plan->getPlanId();poCola->m_lPlanElementId=plan->getPlanElementId();poCola->m_lPlanElementBaseId=plan->getPlanElemen

c++ - 'numeric_limits' 未在此范围内声明,没有用于调用 ‘max()’ 的匹配函数

我在家里的macw/xcode上编译了这段代码,没有任何问题。我在学校用linux上的g++编译它,我得到了这些错误:numeric_limits’isnotamemberofstdexpectedprimary-expressionbefore‘>’tokennomatchingfunctionforcallto‘max()’#include#includeusingnamespacestd;intGetIntegerInput(intlower,intupper){intinteger=-1;do{cin>>integer;cin.clear();cin.ignore(std::n