narrowing

Optional<> and return type narrowing

蓝咒 提交于 2019-12-05 04:45:43
In Java < 8, returning "unsafe" objects (objects or null), I was able to specialize return type in subclass: class A {} class B extends A {} interface Sup { A a(); /* returns A instance, or null */ } interface Sub extends Sup { B a(); } In Java 8, if I want to make my API "safer", I should return Optional<A> instead of "raw" A : interface Sup { Optional<A> a(); } interface Sub extends Sup { Optional<B> a(); } But doesn't compile! Because Optional<B> is not a subclass of Optional<A> . How I'm supposed to resolve this issue? You could use wildcards. interface Sup { Optional<? extends A> a(); }

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

霸气de小男生 提交于 2019-12-05 04:30:30
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. Narrowing a data type in an initialization-list makes your c++11 program ill formed, in this situation the compiler can either give a warning or keep going. Interestingly enough

Why doesn't C++ show a narrowing conversion error when casting a float to a char?

倖福魔咒の 提交于 2019-12-04 04:57:25
Compiling this code using g++ -std=c++17 -Wall -pedantic main.cpp doesn't produce any warnings: #include <iostream> #include <stdlib.h> int main(int argc, char const *argv[]) { for (int i = 0; i < 100; ++i) { float x = 300.0 + rand(); char c = x; std::cout << c << std::endl; } return 0; } Shouldn't it produce a narrowing error? I did some research and I found that -Wall doesn't warn about type conversion issues. Instead, use the flag -Wconversion in order to get a warning about potential type conversion issues. Remarks: For the users of VC++, /W4 will warn you about possible loss of data

TypeScript type inference/narrowing challenge

非 Y 不嫁゛ 提交于 2019-12-01 18:22:25
I'm currently trying to improve the types on some existing code. My code looks roughly like this: /* dispatcher.ts */ interface Message { messageType: string; } class Dispatcher<M extends Message> { on< MessageType extends M["messageType"], SubMessage extends M & { messageType: MessageType } >( messageType: MessageType, handler: (message: SubMessage) => void ): void { } } /* messages.ts */ interface AddCommentMessage { messageType: "ADD_COMMENT"; commentId: number; comment: string; userId: number; } interface PostPictureMessage { messageType: "POST_PICTURE"; pictureId: number; userId: number;

TypeScript type inference/narrowing challenge

你说的曾经没有我的故事 提交于 2019-12-01 17:16:53
问题 I'm currently trying to improve the types on some existing code. My code looks roughly like this: /* dispatcher.ts */ interface Message { messageType: string; } class Dispatcher<M extends Message> { on< MessageType extends M["messageType"], SubMessage extends M & { messageType: MessageType } >( messageType: MessageType, handler: (message: SubMessage) => void ): void { } } /* messages.ts */ interface AddCommentMessage { messageType: "ADD_COMMENT"; commentId: number; comment: string; userId:

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

孤人 提交于 2019-11-30 17:35:48
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? 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-style" (since C++98) initialization forms which use parentheses and equal-sign like int val = 42; A a(val); A a

warning: narrowing conversion C++11

孤街醉人 提交于 2019-11-30 17:25:59
g++ 4.9.0 -O2 -std=c++11 template<class T> struct vec3 { T x, y, z; vec3() = default; vec3(const vec3<T> &other) = default; vec3(T xx, T yy, T zz) { x = xx; y = yy; z = zz; } vec3<T> operator-(const vec3<T> &other) { return vec3<T>{ x - other.x, y - other.y, z - other.z }; } }; int main() { vec3<char> pos{ 0, 0, 0 }; vec3<char> newPos{ 0, 0, 0 }; auto p = pos - newPos; return 0; } I get the warning : !!warning: narrowing conversion of ‘(((int)((vec3<char>*)this)->vec3<char>::x) - ((int)other.vec3<char>::x))’ from ‘int’ to ‘char’ inside { } [-Wnarrowing] But if I do it with (...) insted of {...

Emacs: same buffer, two windows, one narrowed, one not

倾然丶 夕夏残阳落幕 提交于 2019-11-30 04:43:55
I find the narrow-to-region command useful, however it applies to the buffer and not to the current window. I'd like to have one window display a narrowed version of the buffer, while the buffer is displayed widened if it occurs in any other window. Is this possible? Try M-x clone-indirect-buffer or C-x 4 c . For details, see Indirect Buffers . 来源: https://stackoverflow.com/questions/2387287/emacs-same-buffer-two-windows-one-narrowed-one-not

warning: narrowing conversion C++11

此生再无相见时 提交于 2019-11-30 00:55:03
问题 g++ 4.9.0 -O2 -std=c++11 template<class T> struct vec3 { T x, y, z; vec3() = default; vec3(const vec3<T> &other) = default; vec3(T xx, T yy, T zz) { x = xx; y = yy; z = zz; } vec3<T> operator-(const vec3<T> &other) { return vec3<T>{ x - other.x, y - other.y, z - other.z }; } }; int main() { vec3<char> pos{ 0, 0, 0 }; vec3<char> newPos{ 0, 0, 0 }; auto p = pos - newPos; return 0; } I get the warning : !!warning: narrowing conversion of ‘(((int)((vec3<char>*)this)->vec3<char>::x) - ((int)other

C++11: “narrowing conversion inside { }” with modulus

六月ゝ 毕业季﹏ 提交于 2019-11-27 16:13:10
I try to compile the following code with gcc and C++11 enabled: unsigned int id = 100; unsigned char array[] = { id % 3, id % 5 }; I get these warnings: narrowing conversion of ‘(id % 3u)’ from ‘unsigned int’ to ‘unsigned char’ inside { } [-Wnarrowing] see demo online Is there a way to help the compiler find out that the result of id % 3 fits into an unsigned char ? In this specific case making id const or constexpr will fix the problem: constexpr unsigned int id = 100; since there is an exception for the case where you have a constant expression whose result after conversion will fit into the