草庐IT

CONSTANT_VALUE

全部标签

c++ - 如何在模板代码中使用编译时常量条件避免 "conditional expression is constant"警告?

考虑代码:templateCByteArrayserialize(constT&value){if(std::is_pod::value)returnserializePodType(value);elseif(std::is_convertible::value)returnserialize(Variant(value));else{assert(0=="Unsupportedtype");returnCByteArray();}}显然,编译器给我这个警告是正确的if(std::is_pod::value)等等,但是我该如何规避呢?我找不到避免这种检查的方法,而且没有statici

c++ - 此错误消息是否正确 : non-type template argument is not a constant expression

以下程序可以使用GCC5.2编译,但不能使用clang3.6:constexprboolflag();templateconstexprbooltest(){returnb;}intmain(){}我用clang得到的错误信息是:main.cpp:3:20:error:non-typetemplateargumentisnotaconstantexpressiontemplate^~~~~~main.cpp:3:20:note:undefinedfunction'flag'cannotbeusedinaconstantexpressionmain.cpp:1:16:note:decla

c++ - 此错误消息是否正确 : non-type template argument is not a constant expression

以下程序可以使用GCC5.2编译,但不能使用clang3.6:constexprboolflag();templateconstexprbooltest(){returnb;}intmain(){}我用clang得到的错误信息是:main.cpp:3:20:error:non-typetemplateargumentisnotaconstantexpressiontemplate^~~~~~main.cpp:3:20:note:undefinedfunction'flag'cannotbeusedinaconstantexpressionmain.cpp:1:16:note:decla

C++1y/C++14 : Assignment to object outside its lifetime is not allowed in a constant expression?

根据当前草案,以下C++14/C++1y程序是否格式错误?#includetemplatestructliteral_array{Tdata[n];};templateconstexprliteral_arrayoperator+(literal_arraya,literal_arrayb){literal_arrayx;for(size_ti=0;ia={1,2,3};constexprliteral_arrayb={4,5};constexprautoc=a+b;}Clangtrunk(在撰写本文时)给出:error:constexprvariable'c'mustbeinitia

C++1y/C++14 : Assignment to object outside its lifetime is not allowed in a constant expression?

根据当前草案,以下C++14/C++1y程序是否格式错误?#includetemplatestructliteral_array{Tdata[n];};templateconstexprliteral_arrayoperator+(literal_arraya,literal_arrayb){literal_arrayx;for(size_ti=0;ia={1,2,3};constexprliteral_arrayb={4,5};constexprautoc=a+b;}Clangtrunk(在撰写本文时)给出:error:constexprvariable'c'mustbeinitia

c++ - 有没有一种正统的方法来避免编译器警告 C4309 - "truncation of constant value"与二进制文件输出?

我的程序执行将二进制数据写入文件的常见任务,符合某种非文本文件格式。由于我正在写入的数据尚未存在于现有block中,而是在运行时逐字节放在一起,因此我使用std::ostream::put()而不是write().我认为这是正常程序。该程序运行良好。它同时使用std::stringstream::put()和std::ofstream::put()以两位十六进制整数作为参数。但是我得到编译器警告C4309:"truncationofconstantvalue"(inVC++2010)每当参数到put()大于0x7f。显然编译器期待signedchar,常数超出范围。但我认为实际上并没有

c++ - 有没有一种正统的方法来避免编译器警告 C4309 - "truncation of constant value"与二进制文件输出?

我的程序执行将二进制数据写入文件的常见任务,符合某种非文本文件格式。由于我正在写入的数据尚未存在于现有block中,而是在运行时逐字节放在一起,因此我使用std::ostream::put()而不是write().我认为这是正常程序。该程序运行良好。它同时使用std::stringstream::put()和std::ofstream::put()以两位十六进制整数作为参数。但是我得到编译器警告C4309:"truncationofconstantvalue"(inVC++2010)每当参数到put()大于0x7f。显然编译器期待signedchar,常数超出范围。但我认为实际上并没有

c++ - C++ 中的 "new (&variable) value;"有什么作用?

假设我在C++程序中有以下代码:Objecta=Object(someParameters);new(&a)Object(someOtherParameters);我的假设是它将a的内容替换为Object(someOtherParameters),避免为声明可能的operator=对象。这是正确的吗? 最佳答案 它叫做placementnew.它在指定内存上调用构造函数,而不是分配新内存。请注意,在这种情况下,您必须在释放分配的内存之前显式调用对象的析构函数。澄清。假设你分配了一些原始内存char*rawMemory=newchar

c++ - C++ 中的 "new (&variable) value;"有什么作用?

假设我在C++程序中有以下代码:Objecta=Object(someParameters);new(&a)Object(someOtherParameters);我的假设是它将a的内容替换为Object(someOtherParameters),避免为声明可能的operator=对象。这是正确的吗? 最佳答案 它叫做placementnew.它在指定内存上调用构造函数,而不是分配新内存。请注意,在这种情况下,您必须在释放分配的内存之前显式调用对象的析构函数。澄清。假设你分配了一些原始内存char*rawMemory=newchar

c++ - 'value initializing' 是什么意思?

这个问题在这里已经有了答案:关闭10年前.PossibleDuplicate:WhatdothefollowingphrasesmeaninC++:zero-,default-andvalue-initialization?如果我有一个类,例如:classInfo{intx;inty;};我曾经创建过一个对象,Info*p=newInfo();Info旁边的括号是否意味着我正在初始化它?它与Info*p=newInfo;有何不同?我知道有一个question它区分新旧C++语言中的不同含义,但我想知道默认值初始化和值初始化之间的语义差异,例如值初始化是否意味着将某些东西初始化为零?