narrowing

Narrowing conversion from char to double

血红的双手。 提交于 2020-12-26 07:28:05
问题 Why there is a warning narrowing conversion from char to double I know already that for const char there will be no warning. There are a lot of answers about that. But I would like to know, why for non-const char there is a "might-narrow" warning? Is it possible that on some systems mantissa is not big to perfectly represent char? int main() { char c{7}; double a{c}; } 4:13: warning: narrowing conversion of 'c' from 'char' to 'double' inside { } [-Wnarrowing] 回答1: It is narrowing because the

Is it possible to avoid static_cast in initializer list?

你说的曾经没有我的故事 提交于 2020-01-13 10:05:38
问题 In my code base I often initialize array or vector if bytes using the following the syntax: uint16_t foo = 0xAB, bar = 0xCD // bytes = { 0xA, 0xB, 0xC, 0xD } std::array<uint8_t, 4> bytes = {{ foo >> 8, foo & 0x00FF, bar >> 8, bar & 0x00FF }}; I get the following error from clang++: error: non-constant-expression cannot be narrowed from type 'int' to 'value_type' (aka 'unsigned char') in initializer list [-Wc++11-narrowing] foo >> 8, ^~~~~~~~~~~~~ The compiler suggest me to add a static_cast

Is it possible to avoid static_cast in initializer list?

两盒软妹~` 提交于 2020-01-13 10:05:10
问题 In my code base I often initialize array or vector if bytes using the following the syntax: uint16_t foo = 0xAB, bar = 0xCD // bytes = { 0xA, 0xB, 0xC, 0xD } std::array<uint8_t, 4> bytes = {{ foo >> 8, foo & 0x00FF, bar >> 8, bar & 0x00FF }}; I get the following error from clang++: error: non-constant-expression cannot be narrowed from type 'int' to 'value_type' (aka 'unsigned char') in initializer list [-Wc++11-narrowing] foo >> 8, ^~~~~~~~~~~~~ The compiler suggest me to add a static_cast

class cast exception in narrow a jndi reffrence in ejb

孤人 提交于 2020-01-03 08:49:09
问题 I am trying to write a simple stateless sesssion bean but I have problem with narrow reference I give in lookup time. I got class cast exeption I use eclipse IDE my bean class package codes; import java.rmi.RemoteException; import javax.ejb.EJBException; import javax.ejb.SessionBean; import javax.ejb.SessionContext; public class SinaBean implements SessionBean { /** * */ private static final long serialVersionUID = 1L; public String getHello() { return "hello"; } public void ejbCreate(){ }

What does this mean “Narrowing a primitive truncates the high order bits”

僤鯓⒐⒋嵵緔 提交于 2019-12-24 03:28:31
问题 What does this mean "Narrowing a primitive truncates the high order bits" 回答1: E.g. if you cast long to int you are discarding the higher bits of the long. Short -> Byte 0x00FF -> 0xFF 256 -> -128 回答2: Here's a short, carefully-chosen answer. public class Narrow { public static void main(String[] args) { int i; short s; i = 32768; s = (short) i; System.out.println("int of " + i + " becomes a short of " + s); } } i is 2^15, or one bigger than the MAX_VALUE of short. Java will reply int of

Create array of chars avoiding narrowing

不想你离开。 提交于 2019-12-22 05:38:09
问题 I am writing a unit test checking some binary data against an expected array. The expected array in question is just some series of bytes, doesn't matter specifically: char expected[] = {0x42, 0xde, 0xad, 0xbe, 0xef}; This compiled fine in C++, but with C++11 this issues a warning on narrowing conversion. I compile with -Werror because warnings matter, so that line does not compile for me. As far as I'm aware, there's no literal suffix for char, so it seems I'd have to do: char expected[] =

Why does `bool b = 2` work well but `bool b = {2}` yield a warning of narrowing conversion?

元气小坏坏 提交于 2019-12-22 04:44:13
问题 Using the {} initializer in C++11 to initialize bool b = {2} yields the following warning message: warning: narrowing conversion of ‘2’ from ‘int’ to ‘bool’ inside { } [-Wnarrowing] However, using the old style bool b = 2 has no such problem. What is the reason behind this? Update: I compiled the code using g++ -std=c++11 and it gave me the warning. If I add the option -pedantic-errors , the warning becomes an error. 回答1: Narrowing a data type in an initialization-list makes your c++11

Why does a narrowing conversion warning appear only in case of list initialization?

杀马特。学长 韩版系。学妹 提交于 2019-12-18 18:53:28
问题 I have the following code: class A { public: A(const unsigned int val) : value(val) {} unsigned int value; }; int main() { int val = 42; A a(val); A b{val}; // <--- Warning in GCC, error in Microsoft Visual Studio 2015 return 0; } Why does the narrowing conversion warning appear only in case of list initialization usage? 回答1: list initialization was introduced since C++11 with the feature prohibiting implicit narrowing conversions among built-in types. At the same time, the other two "old

Why isn't a final variable always a constant expression?

[亡魂溺海] 提交于 2019-12-18 12:04:32
问题 In the below code: final int a; a=2; byte b=a; // error: possible loss of precision Why do I get this error? Isn't a final variable compile time constant expression and hence implicitly narrowed to byte during the assignment? In other words isn't the above code equivalent to: final int a=2; byte b=a; 回答1: The compiler isn't that smart. We can tell that the value will always be 2. But what if we had something like this? class ABC{ final int a; public ABC(){ if(Math.random() < .5){ a = 2; }

Why isn't a final variable always a constant expression?

。_饼干妹妹 提交于 2019-12-18 12:04:10
问题 In the below code: final int a; a=2; byte b=a; // error: possible loss of precision Why do I get this error? Isn't a final variable compile time constant expression and hence implicitly narrowed to byte during the assignment? In other words isn't the above code equivalent to: final int a=2; byte b=a; 回答1: The compiler isn't that smart. We can tell that the value will always be 2. But what if we had something like this? class ABC{ final int a; public ABC(){ if(Math.random() < .5){ a = 2; }