草庐IT

returning

全部标签

C++11 可变参数模板 : return tuple from variable list of vectors

我想写一些类似于pythonzip(http://docs.python.org/2/library/functions.html)的东西。zip应该接受可变数量的不同类型的vector,并返回一个vector元组,截断到最短输入的长度。例如x=[1,2,3]v=['a','b']我希望输出是一个vector[,]如何在C++11中做到这一点? 最佳答案 急切地做到这一点并且只通过复制非常容易:#include#include#includetemplatestd::vector>zip(std::vectorconst&...vs

c++ - 另一个C++学习时刻 : returning strings from functions

我有一些关于C++的基本问题。考虑以下我尝试返回字符串的代码。conststd::string&NumberHolder::getValueString(){charvalueCharArray[100];sprintf_s(valueCharArray,"%f",_value);std::stringvalueString(valueCharArray);returnvalueString;}我正在尝试返回一个字符串,其中包含名为_value的类成员的值。但是,我收到警告说我正在尝试将指针传回局部变量。这当然是一件坏事。如果此时我对C++有足够的了解,这意味着当有人尝试使用它时,我传

c++ - ARM C++ ABI : Constructor/destructor return values

我一直在阅读Clang源代码,并发现了一些关于ARMC++ABI的有趣之处,我似乎无法理解其理由。来自ARMABIdocumentation的在线版本:ThisABIrequiresC1andC2constructorstoreturnthis(insteadofbeingvoidfunctions)sothataC3constructorcantailcalltheC1constructorandtheC1constructorcantailcallC2.(对于非虚拟析构函数也是如此)我不确定C1、C2和C3在这里引用什么...本节旨在修改来自通用(即安腾)ABI的第3.1.5节,但

c++ - g++ 错误? ( bool 值?0 : 1) returns neither 0 nor 1

我通过这个简单的演示重现了这个问题://bool_test_func.cpp#includevoidfunc(bool*b){inta=(*b?0:1);printf("%d\n",a);//EXPECTether0or1here}//bool_test.cppvoidfunc(bool*b);intmain(){intn=128;func((bool*)&n);return0;}-O0编译运行:g++-g-O0-Wall-obool_testbool_test.cppbool_test_func.cppmikewei@maclinux:~/testing/c++$./bool_tes

c++ - 为什么在没有明确的 return 语句的情况下,递归的 return 调用会脱离堆栈?

我看到了一个示例程序来演示递归,它看起来不应该工作,但确实有效。逻辑很清楚,但为什么即使没有返回递归函数调用,它也能工作呢?即使没有请求,似乎return命令也会脱离堆栈。这是语言标准还是gcc的东西?我在Windows和Linux上看到了用gcc编译的C和C++。#include#includeusingnamespacestd;intisprime(intnum,inti){if(i==1){return1;}else{if(num%i==0)return0;elseisprime(num,i-1);//shouldbereturned}}intmain(intargc,char*

C++ 编译器错误 : "return type specification for constructor invalid"

这是我的代码。编译所有文件时出现此错误,我不确定自己做错了什么。请指教。Molecule.cpp:7:34:error:returntypespecificationforconstructorinvalid//SunnyPathak//Molecule.cpp#include#include"Molecule.h"usingnamespacestd;inlinevoidMolecule::Molecule(){intcount;count=0;}//endfunctionboolMolecule::read(){cout 最佳答案

c++ - return {} 和 return Object{} 之间的区别

这两个功能有什么明显的区别吗?structObject{Object(inti):i{i}{}inti;};Objectf(){return{1};}Objectg(){returnObject{1};} 最佳答案 第一个是copy-list-initialization,将选择合适的构造函数(即Object::Object(int))来构造返回值。第二个将通过direct-list-initialization构造一个临时Object,(也调用Object::Object(int)),然后将其复制到返回值。因为copyelisio

return 语句中的 C++ constexpr 函数

为什么constexpr函数在编译时不求值,而在运行时在main函数的return语句中求值?试过了templateconstexprintfac(){returnfac()*x;}templateconstexprintfac(){return1;}intmain(){constintx=fac();returnx;}结果是main:pushrbpmovrbp,rspmovDWORDPTR[rbp-4],6moveax,6poprbpret使用gcc8.2。但是当我在return语句中调用函数时templateconstexprintfac(){returnfac()*x;}temp

c++ - 使用 C 字符串会给出警告 : "Address of stack memory associated with local variable returned"

我不是C程序员,所以我对C-string不是很熟悉,但是现在我必须使用C库,所以这里是我的代码的简化版本来演示我的问题:char**ReadLineImpl::my_completion(){char*matches[1];matches[0]="add";returnmatches;}我收到此警告:Warning-addressofstackmemoryassociatedwithlocalvariable'matches'returned而且我的程序似乎不能正常工作(可能是因为上面提到的警告)。警告意味着什么?会不会造成什么问题? 最佳答案

c++ - 在 C++ 程序的 main 函数中, `return 0` 是什么意思?

这个问题在这里已经有了答案:关闭10年前。PossibleDuplicate:Whatistheproperdeclarationofmain?没有特别引用任何代码,我正在寻找对以下示例的解释:#includeintmain(){std::cout我不明白return0的作用。你能用尽可能简单的英语解释一下吗? 最佳答案 这定义了exitstatus的过程。尽管是int,在类Unix系统上,该值始终在0-255范围内(参见ExitandExitStatus)。在Microsoft系统上,您可以使用32-bitsignedintege