草庐IT

C++ : Different deduction of type auto between const int * and cont int &

这里是代码示例。a.intii=0;b.constintci=ii;c.autoe=&ci;-->eisconstint*d.auto&f=42;-->invalidinitializationofnon-constreferenceoftype‘int&’fromanrvalueoftype‘int’e.constauto&g=42-->ok观察:1.对于c)子句,自动推导类型const2.对于子句d),不会自动推导出类型const3.对于条款e),必须手动添加类型const才能使其工作。为什么子句c而不是d会自动推导类型const? 最佳答案

c++ - 错误 C4430 缺少类型说明符 - 假定为 int。注意 : C++ does not support default-intGenerator

我对以下代码有疑问:生成器.h:#pragmaonceclassGenerator{public:friendclassBagObject;Generator(void);~Generator(void);...voidgenerator(int);private:BagObject*object;vectordata;//Errorc4430};这是一个错误:errorC4430:missingtypespecifier-intassumed.Note:C++doesnotsupportdefault-int还有6个错误,但我相信在解决这个问题后它们应该会消失。这是cpp文件。第一次

c++ - 有没有比添加 0.5f 和截断转换更直接的方法来将 float 转换为 int 并进行舍入?

在处理浮点数据的C++代码中,从float到int的舍入转换经常发生。例如,一种用途是生成转换表。考虑这个代码片段://ConvertapositivefloatvalueandroundtothenearestintegerintRoundedIntValue=(int)(FloatValue+0.5f);C/C++语言将(int)转换定义为截断,因此必须添加0.5f以确保向上舍入到最接近的正整数(当输入为正时)。对于以上,VS2015的编译器生成如下代码:movssxmm9,DWORDPTR__real@3f000000//0.5faddssxmm0,xmm9cvttss2siea

c++ - 为什么 uint8_t + uint8_t 会产生一个 int?

这个问题在这里已经有了答案:Additionoftwocharsproducesint(3个答案)关闭3年前。我注意到,对于8位变量的简单操作,变量在操作完成之前转换为32位int,然后再转换回8位变量。例如,这个C++程序:#include#includeintmain(void){uint8_ta=1;uint8_tb=2;std::cout产生以下输出:sizeof(a)=1sizeof(b)=1sizeof(a+b)=4所以我们可以理解发生的事情是:uint8_tc=(uint8_t)((int)(a)+(int)(b));显然,它似乎在this中所说的C规范中。论坛。此外,在

c++ - 统一初始化int* : how can it be forced?

以下代码无法使用clang++3.8.0和g++6.3.0进行编译(编译器标志为-std=c++11-Wall-Wextra-Werror-pedantic-errors):intmain(){int*a=int*{};//doesn'tcompile//^^^^can'tbeparsedasatype(void)a;usingPInt=int*;PIntb=PInt{};//compilessuccessfully//^^^^isparsedasatype(void)b;}这是一种强制编译器以正确方式解释int*{}的方法吗(typedefingofint*是其中一种方式)?

c++ - switch 语句和对象隐式 int 转换

在C++中,直接在隐式转换为int的对象上使用switch语句是否合法/正确?而不是使用返回对象标记的方法。classAction{public:enumEType{action1,action2,action3};operatorint()const{returnmType;}private:ETypemType;/*...*/}intmain(){Actiona=/*...*/switch(a){caseAction::EType::action1:/*...*/break;caseAction::EType::action2:/*...*/}} 最佳答

c++ - 将 int 数组转换为 char*

这可能吗?我想将其转换为char*,以便稍后检索该值。 最佳答案 当然:intarray[4]={1,2,3,4};char*c=reinterpret_cast(array);有效范围是从c到c+sizeof(array)。您可以对任何POD类型执行此操作。您可以从字节序列中强制转换://assumingcaboveint(&pArray)[4]=*reinterpret_cast(c);这保证有效。但是,您似乎正试图通过网络发送内容,这可能会引入其他问题您正在寻找的过程称为序列化(并且有一个FAQentry)。这是当您获取一个对

c++ - 通过在 Fortran 中调用 C++ 函数将 Fortran int 数组传递给 C++

我正在尝试在Fortran子例程中调用C++函数。这个C++函数应该更新一个整数数组。这是我写的一个非工作代码。什么问题?!FortranfunctionthatcallsaC++function.subroutinemy_function()integer(4)ar(*)integer(4)get_filled_ar!Needcorrectsyntaxhere.ar=get_filled_ar()end//C++function:extern"C"{voidget_filled_ar(int*ar){ar[0]=1;ar[1]=10;ar[3]=100;}}

c++ - 如何在不创建空类的情况下避免专门化 "big"模板类?

鉴于以下情况:templateclassTuple{private:T0v0;T1v1;T2v2;T3v3;T4v4;public:voidf(){cout我想创建一个只有两个int的部分类-s,那么我必须像这样专门化:classNullType{};//createanemptyclasstemplateclassTuple{private:T0v0;T1v1;public:voidfunc(){cout但是这个实现需要我做:Tupleb;所以这很丑:)是否有另一种方法可以在不定义另一个(空)类的情况下实现部分特化,这样我就可以做到:Tupleb1;? 最

c++ - operator++()和operator++(int)有什么区别?

这个问题在这里已经有了答案:Overloading++forbothpreandpostincrement(4个答案)关闭9年前。我有我老师制作的程序中的这些行代码:TimeKeeper&operator++(){d_seconds++;return*this;}constTimeKeeperoperator++(int){TimeKeepertk(*this);++(*this);returntk;}我的老师问我们的问题之一是“operator++()返回一个引用而operator++(int)返回一个值,请解释为什么?”谁能给我解释一下??如果您需要其余的代码,我不介意把它放在上面