草庐IT

return_var

全部标签

c++ - `type *var = (int)0` ,合法与否?

下面的例子:char*var=(int)0;在gcc和cl.exe上编译,但在clang中导致错误:cannotinitializeavariableoftype'char*'withanrvalueoftype'int'谁是正确的?对于它的值(value),C++11说(强调我的)4.10/1Anullpointerconstantisanintegralconstantexpression(5.19)prvalueofintegertypethatevaluatestozerooraprvalueoftypestd::nullptr_t.Anullpointerconstantca

c++ - return *this 在 C++ 中安全吗?

我想知道从函数返回*this是否安全。this问题显示了一些你可以做到的方法,我的问题是这个例子:structtest{stringt;stringb;public:test&A(stringtest){this->t=test;return*this;}test&B(stringtest){this->b=test;return*this;}};intmain(){autoa=test().A("a").B("b").A("newa");return0;}会不会有内存泄漏? 最佳答案 isreturn*thissafeinc++基

c++ - 哪个更好 : returning tuple or passing arguments to function as references?

我创建的代码中有两个函数returnValues和returnValuesVoid。一个返回2个值的元组,另一个接受参数对函数的引用。#include#includestd::tuplereturnValues(constinta,constintb){returnstd::tuple(a,b);}voidreturnValuesVoid(int&a,int&b){a+=100;b+=100;}intmain(){auto[x,y]=returnValues(10,20);std::cout我读到了http://en.cppreference.com/w/cpp/language/st

c++ - Const 接收一个 var,我不能将它传递给模板

我想做的是:intconstbitsPerInt=log2(X);bitsetbits(arandomnumber...);但是我得到这个错误:'bitsPerInt'cannotappearinaconstantexpressionerror:templateargument1isinvalid 最佳答案 如果你真的需要它工作,制作你自己的在编译时工作的log2并将它传递给bitset的模板参数。constexprunsignedLog2(unsignedn,unsignedp=0){return(nbits;Liveexampl

c++ - 谷歌模拟 : Return() a list of values

通过GoogleMock的Return(),您可以返回调用模拟函数后将返回的值。但是,如果期望某个函数被调用多次,并且每次都希望它返回不同的预定义值。例如:EXPECT_CALL(mocked_object,aCertainFunction(_,_)).Times(200);如何让aCertainFunction每次都返回一个递增的整数? 最佳答案 使用sequences:using::testing::Sequence;Sequences1;for(inti=1;i 关于c++-谷歌模

c++ - 编译器警告 : lambda return type cannot be deduced

考虑这个例子:#include#includeintmain(){std::stringstr="abcde4fghijk4l5mnopqrs6t8uvwxyz";std::stringstr2;std::remove_copy_if(str.begin(),str.end(),std::back_inserter(str2),[](char&c){if(std::isdigit(c))returntrue;//使用GCC4.6.1,这可以很好地编译并打印出预期的输出(字母表),但我收到一条警告说“只有当return语句是函数体中的唯一语句时,才能推导出lambda返回类型”.现在,我

c++ - 这个return语句有什么意义吗?

我想知道我看到的一段代码是否有任何意义return(num!=0);其中num是一个整数。这是一个boolean函数的返回语句,如果num!=0则返回TRUE,如果num=0则返回false。我不确定这是否有隐藏的意义,但我不明白为什么他们不能简单地写:returnnum;这是我看到的代码:boolSemClass::cut(int&a,int&b,int&c){intnum=0;check(a,num);check(b,num);check(c,num);return(num!=0);} 最佳答案 当通过隐式转换作为boolean

c++ - 什么是 T (& var)[N]?

在boost/utility/swap.hpp中我找到了这段代码:templatevoidswap_impl(T(&left)[N],T(&right)[N]){for(std::size_ti=0;i什么是左和右?它们是对数组的引用吗?C++ISO标准2003或更高版本是否允许此代码? 最佳答案 对类型T和长度N的数组的引用。这是C的指针数组语法的自然扩展,并受C++03支持。你可以使用cdecl.org尝试解析这些复杂的类型声明。 关于c++-什么是T(&var)[N]?,我们在St

c++ - return 语句中可以省略 return 关键字吗?

我最近在这个ApacheAxistutorialexample.中看到了下面的一段代码intmain(){intstatus=AXIS2_SUCCESS;axutil_env_t*env=NULL;axutil_allocator_t*allocator=NULL;env=create_environment();status=build_and_serialize_om(env);(status==AXIS2_FAILURE){printf("buildAXIOMfailed");}axutil_env_free(env);0;}我不明白的是最后的0;。那个return语句没有ret

C++ 标准 : return by copy to initialize a reference without RVO: is there any copy?

让我们考虑下一个示例:structbig_type{};//Returnbycopyautofactory(){returnbig_type{};}voidany_scope_or_function(){big_type&&lifetime_extended=factory();}假设RVO被禁止或根本不以任何方式存在,big_type()是否会或可以被复制?还是将引用直接绑定(bind)到return语句中构造的临时对象?我想确保big_type析构函数仅在any_scope_or_function结束时被调用一次。我使用C++14,以防某些行为在标准版本之间发生变化。