草庐IT

returner

全部标签

c++ - 重载运算符 : const vs non-const return type : any difference of performance?

如果我们去维基百科article关于C++运算符,我们有一个例子:Addition:a+b->TT::operator+(constT&b)const;因此运算符返回类型为T的非常量。如果我们看这个guideline作者说返回类型应该是const以避免以下语法:(a+b)=c现在假设我不介意这种语法,并考虑a和b是大数组。从“纯”性能的角度来看,返回类型中缺少const关键字是否会阻止编译器的优化(g++和带有-O3的英特尔icpc)?如果答案是"is",为什么? 最佳答案 这是一个有趣的问题。在C++03中,没有更好的机会使用这两

c++ - 复制省略 : move constructor not called when using ternary expression in return statement?

考虑以下示例:#includeclassobject{public:object(){printf("constructor\n");}object(constobject&){printf("copyconstructor\n");}object(object&&){printf("moveconstructor\n");}};staticobjectcreate_object(){objecta;objectb;volatileinti=1;//With#if0,object'scopyconstructoriscalled;otherwise,itsmoveconstructor

c++ - 关于-Waggregate-return 的目的/行为的混淆?

同时查看GCC'swarningoptions,我遇到了-Waggregate-return。-Waggregate-returnWarnifanyfunctionsthatreturnstructuresorunionsaredefinedorcalled.(Inlanguageswhereyoucanreturnanarray,thisalsoelicitsawarning.)引发警告的小例子:classfoo{};foof(void){returnfoo{};}intmain(){}$g++-std=c++0x-Waggregate-return-omainmain.cppmai

c++ - 自动返回类型扣除警告 : why do we need decltype when return defines the type anyway?

这是一个关于elementsSize()成员函数做什么的问题,关于自动返回类型推导:#include#includetemplateclassElementVector{std::vectorelementVec_;//Otherattributes.public:ElementVector()=default;ElementVector(conststd::initializer_list&list):elementVec_(list){}autoelementsSize()//->decltype(elementVec_size()){returnelementVec_.size(

c++ - 构建 Qt - NMAKE : fatal error U1077: 'cd' : return code '0x2'

我正在尝试使用msvc2015通过静态链接构建Qt5.5,但我遇到了以下错误(实际上有很多错误,但我只列出了一些,它们都是一样的):K:\Archivosdeprograma\MicrosoftVisualStudio14.0\VC\INCLUDE\cstdint(50):errorC2873:'uint_fast64_t':symbolcannotbeusedinausing-declarationK:\Archivosdeprograma\MicrosoftVisualStudio14.0\VC\INCLUDE\cstdint(52):errorC2039:'intmax_t':i

C++ 函数模板 : Derived and explicit return type

我有以下问题,我只是没有找到合适的解决方案(也许没有):我有一个模板化方法,其中返回类型取决于输入类型,感谢C++11decltype返回类型可以很容易地导出,但是如果需要,我还希望允许用户明确定义返回类型。更正式地说,我有一个模板化函数f,我想被调用为f(x),既没有显式定义输入类型,也没有显式定义返回类型。我也希望能够将其称为fx()明确定义返回类型,但输入类型仍然自动派生。现在,用C++11满足第一个约束很容易(假设还有另一个模板化方法:templateautof(constInT&in)->decltype(/*codederivingthereturntypeusingin*

c++ - 学习 C++ : returning references AND getting around slicing

我很难理解引用文献。考虑以下代码:classAnimal{public:virtualvoidmakeSound(){coutmakeSound();Animal&r1=rFunc();r1.makeSound();Animalr2=rFunc();r2.makeSound();Animalv=vFunc();v.makeSound();}结果是:“barkbarkrawrrawr”。以Java的思维方式(这显然破坏了我对C++的概念化),结果将是“barkbarkbarkbark”。我从我的previousquestion了解到这种差异是由于切片造成的,我现在对切片是什么有了很好的理

c++ - 继承 : Function that returns self type?

假设我有两个类:classA{public:A*Hello(){returnthis;}}classB:publicclassA{public:B*World(){returnthis;}}假设我有一个B类的实例,如下所示:Btest;如果我调用test.World()->Hello()就可以了。但是test.Hello()->World()将无法工作,因为Hello()返回A类型。如何让Hello()返回B的类型?我不想使用virtual函数,因为我们有20多个不同的类继承A。 最佳答案 您可以使用CRTP,奇怪的重复模板模式:t

c# - 来自 C# : C++ function (in a DLL) returning false, 的 C++,但 C# 认为这是真的!

我正在编写一个C#小应用程序,它调用C++API中的几个函数。我将C++代码构建到DLL中,C#代码使用DllImport调用API。(我正在为C++DLL使用.DEF文件,所以我不需要extern"C"。)到目前为止,API只有一个函数,目前它什么都不做:boolFoo(){returnfalse;}在C#中,我有以下内容:publicclassFooAPI{[DllImport("Foo.dll")]publicstaticexternboolFoo();}...boolb=FooAPI.Foo();if(!b){//Throwanexception}我的问题是,出于某种原因,b的

windows - 创建DIBSection : Return value vs error

ThedocumentationofCreateDIBSection状态:Ifthefunctionsucceeds,thereturnvalueisahandletothenewlycreatedDIB,and*ppvBitspointstothebitmapbitvalues.Ifthefunctionfails,thereturnvalueisNULL,and*ppvBitsisNULL.然而,紧随其后的是:Thisfunctioncanreturnthefollowingvalue[...]ERROR_INVALID_PARAMETER那么,最后这句话是什么意思呢?我很难想象它