草庐IT

freshplanet-as

全部标签

c++ - std::tie 和 std::forward_as_tuple 有什么区别

对于一个给定的类,如果我想写所有的比较运算符,为了避免代码重复,我会这样写:classB{public:booloperator==(Typeconst&rhs)const{returnas_tuple()==rhs.as_tuple();}booloperator!=(Typeconst&rhs)const{returnas_tuple()!=rhs.as_tuple();}//..andsameforotheroperators..private:autoas_tuple()const{returnstd::tie(a,b,c);//allthemembers}};我可以用std:

C++ 重载 new[] 查询 : What size does it take as parameter?

我像这样重载了operatornew[]void*human::operatornew[](unsignedlongintcount){cout现在打电话human*h=newhuman[14];说sizeof(human)=16,但计算它打印出来的是232,也就是14*16+sizeof(int*)=224+8。为什么要分配这个额外的空间?它落在内存中的什么地方?因为当我打印*h或h[0]我得到相同的结果,所以它不在内存块的开头。它是否完全正确,或者我在这里遗漏了一些东西? 最佳答案 分配的额外空间用于存储内部使用的数组大小(在实

c++ - Visual Studio : How to use platform toolset as preprocessor directive?

我的项目有两个平台工具集:v110和v110_xp,根据所选平台,我想包含/排除部分要编译的代码。_MSC_FULL_VER和$(PlatformToolsetVersion)对于这两个平台工具集具有完全相同的值。或者,我尝试使用$(PlatformToolset)如下:_MSC_PLATFORM_TOOLSET=$(PlatformToolset)但问题是$(PlatformToolset)是非数字的。想知道如何将这个非数字值用作预处理器指令?尝试了几种解决方案后我发现了_MSC_PLATFORM_TOOLSET='$(PlatformToolset)'然后#if(_MSC_PLAT

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++ : Why cant static functions be declared as const or volatile or const volatile

这个问题在这里已经有了答案:关闭10年前。PossibleDuplicate:C++-Whystaticmemberfunctioncan’tbecreatedwith‘const’qualifier想知道为什么静态成员函数不能声明为const或volatile或constvolatile的原因?#includeclassTest{staticvoidfun()const{//compilererrorreturn;}};

c++ - 我可以从 "treat warnings as errors"中排除一些特定的警告而不禁用它们吗?

在我的VisualC++代码中,我想要/WX-“将警告视为错误”。这让我处理每个警告,包括C4996-“Xwasdeclareddeprecated”我不想解决-我现在不想更改代码,我不想禁用C4996以便它保留在输出中。所以理想情况下我想要这样的东西:#pragmawarning(ExcludeFromWX:4996)因此,当使用/WX时,除此之外的所有警告都被视为错误,并且仅显示此警告并继续编译。是否有可能得到这样的行为? 最佳答案 您可以使用以下pragma重置指定的警告。我没有测试过,你也没有提到尝试这个:更新更改警告级别应

C++ "error: passing ' const std::map<int, std::basic_string<char>>' as ' this' argument of ..."

使用以下代码(为简洁起见摘录):颜色.h:classcolor{public:color();enumcolorType{black,blue,green,cyan,red,magenta,brown,lightgray,nocolor};colorTypegetColorType();voidsetColorType(colorTypecColortype);stringgetColorText()const;private:colorTypecColortype=nocolor;mapcolors={{black,"black"},{blue,"blue"},{green,"gre

c++ - "error: cannot use type ' void' as a range"究竟是什么意思?

当我在clang3.2中编译它时for(autox:{1,1.2}){}我收到这样的错误:error:cannotusetype'void'asarange这是什么意思? 最佳答案 您在初始化列表中混合了您的类型。在这种情况下它可以很清楚,但不要忘记std::stringfoo;for(autox:{foo,"bar"}){}也是两种不同的类型。当然还有很多其他情况,您可能希望它起作用,但类型必须完全匹配。 关于c++-"error:cannotusetype'void'asarange

c++ - 为什么这有效 : C++ last statement as result of expression

这个问题在这里已经有了答案:Arecompoundstatements(blocks)surroundedbyparensexpressionsinANSIC?(2个答案)Warning"UseofGNUstatementexpressionextension"(4个答案)关闭6年前。我在驱动程序实现中发现了奇怪的宏,我无法向自己解释。简化的例子是:cout它将输出10。但是为什么expression变成了右值呢?它似乎适用于C和C++。有人可以解释一下吗?指向关键字和引用会很棒。

c++ - 如何将 "warnings as error"规则添加到 Qt .pro 文件?

当我通常处理C++项目时,我做的第一件事就是在我的编译器上设置“将警告视为错误”。当使用Qt时,qmake会为您生成Makefile,并且不会在编译命令中包含此选项。我很确定有一种方法可以将这样的选项(和其他选项)添加到生成的Makefile中,但我想不通。我该怎么做?我使用Qt的开源版本和g++作为编译器。 最佳答案 您可以使用QMAKE_CXXFLAGS在pro文件中指定编译器标志:QMAKE_CXXFLAGS+=-Werror 关于c++-如何将"warningsaserror"规