草庐IT

gravity_value_t

全部标签

没有宏的 C++ 简单反射 : Print Variable Name and Its Value

在C++中是否有一种非宏的方式来打印变量名及其值。这是宏方法:#defineSHOW(a)std::coutPS:我用的是Linux,不需要跨平台的解决方案 最佳答案 不,C++不支持反射,唯一的方法(据我所知)是使用宏。 关于没有宏的C++简单反射:PrintVariableNameandItsValue,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/6849965/

c++ - 从 int 转换为 std::array<unsigned char, 1ul>::value_type 可能会改变其值

编译此程序时,-WconversionGCC参数产生标题中的警告:#include#include#includeintmain(){std::stringtest="1";std::arraybyteArray;byteArray[0]=byteArray[0]|test[0];return0;}这是我编译它的方式:g++--Wall-Wextra-Wconversion-pedantic-std=c++0xtest.cpp我使用的是GCC4.5。我在这里做违法的事情吗?它会在某些情况下引起问题吗?为什么|会产生一个int? 最佳答案

Kotlin:乐趣与值(value)

Kotlin支持计算属性,但我不确定何时使用它们。假设我有一个类:classCar(valcolor:String)如果汽车是白色的,则具有返回true的函数:funisWhite(car:Car):Boolean{returncar.color=="WHITE"}现在我希望这个函数成为一个成员函数(一个方法);这看起来像这样:classCar(valcolor:String){funisWhite():Boolean=color=="WHITE"}但它也可以是这样的:classCar(valcolor:String){valisWhite:Booleanget()=color=="W

Kotlin:乐趣与值(value)

Kotlin支持计算属性,但我不确定何时使用它们。假设我有一个类:classCar(valcolor:String)如果汽车是白色的,则具有返回true的函数:funisWhite(car:Car):Boolean{returncar.color=="WHITE"}现在我希望这个函数成为一个成员函数(一个方法);这看起来像这样:classCar(valcolor:String){funisWhite():Boolean=color=="WHITE"}但它也可以是这样的:classCar(valcolor:String){valisWhite:Booleanget()=color=="W

c++ - 了解警告 : binding r-value to l-value reference

我想通过引用传递一个结构,这样它就不会被复制,但Resharper发出以下警告:structsometype{};sometypefoo(){sometypex;returnx;}voidbar(){sometype&a=foo();//Bindingr-valuetol-valuereferenceisnon-standardMicrosoftC++extensionsometype&&b=foo();//ok}问题:sometype&a=foo();有什么问题?foo()的返回值不是左值,a不是左值吗?sometype&&b=foo();实际上是右值引用吗?它是否“窃取”了foo(

c++ - 为什么 myClassObj++++ 不会产生编译错误 : '++' needs l-value just as buildin type do?

为什么myint++++使用VS2008编译器和gcc3.42编译器编译得很好??我期待编译器说需要左值,示例见下文。structMyInt{MyInt(inti):m_i(i){}MyInt&operator++()//returnreference,returnalvalue{m_i+=1;return*this;}//operator++needit'soperandtobeamodifiablelvalueMyIntoperator++(int)//returnacopy,returnarvalue{MyInttem(*this);++(*this);returntem;}in

C++14 对 is_same::value 使用别名

Thisquestion使用is_same::value.我希望有一个C++14usingalias:is_same_v类似于辅助类型:conditional_t,enable_if_t,和tuple_element_t我在myanswer中使用.因为我使用这些函数中的任何一个的唯一目的就是获取type.所以*_t助手才有意义。这让我想到了我的问题,为什么没有usingaliasis_same_v在C++14中?我唯一用的is_same因为它是value.也许使用is_same通常不是用于模板声明吗? 最佳答案 简介引入std::e

c++ - 为什么 boost::is_same<int const&, boost::add_const<int &>::value 等于 false?

我正在处理"C++TemplateMetaprogramming"byAbrahams&Gurtovoy“这实际上不在第二章中,而是我在做第一个练习(2.10、2.0)时尝试过的,这让我很困惑:#include#includestd::stringdisplay(boolb){return(b?"true":"false");}intmain(){usingnamespacestd;cout::type>::value)输出为“假”。但是,如果我删除引用,即“intconst”和“int”。输出为“真”。 最佳答案 如果你用指针尝试

c++ - Lambda Capture by Value 强制所有作用域对象为 const

我打算用C++编写一个内存模式,结果采用了以下方法std::functionMemoize(std::functionfn){std::mapmemo;std::functionhelper=[=](intpos){if(memo.count(pos)==0){memo[pos]=fn(pos);}returnmemo[pos];};returnhelper;}奇怪的是,我的编译器VS2012,拒绝编译并出现以下错误1>Source1.cpp(24):errorC2678:binary'[':nooperatorfoundwhichtakesaleft-handoperandoftyp

c++ - 一般规则 : negative or positive values for error code in C/C++

按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visitthehelpcenter指导。关闭10年前。我有自己的返回类型和函数定义如下:typedefenumxx_return_t{success=0,general_error=-1,specific_error=-2,[...]}xx_return_type;xx_return_typegeneralFunction(void){if(there_was_an_error)returngeneral_