primitive

Should I really use static_cast every single time I want to convert between primitive types?

好久不见. 提交于 2019-12-04 02:52:41
What makes this long l = 1; char c = static_cast<char>(l); float f = 1.0f; int i = static_cast<int>(f); better than this long l = 1; char c = (char)l; float f = 1.0f; int i = (int)f; when casting one primitive data type to another? I've got much of legacy code that uses the second style for type casting in similar situations, so this is also a question about should I or may I not perform full-scale revision of that code. R. Martinho Fernandes Future-proofing. Let's say in the future I do this: float blah = 1.0f; float* f = &blah; Now, int i = static_cast<int>(f); stops compiling, but int i =

Anyone using short and byte primitive types, in real apps?

北城以北 提交于 2019-12-04 01:46:01
I have been programming in Java since 2004, mostly enterprise and web applications. But I have never used short or byte , other than a toy program just to know how these types work. Even in a for loop of 100 times, we usually go with int . And I don't remember if I have ever came across any code which made use of byte or short , other than some public APIs and frameworks. Yes I know, you can use a short or byte to save memory in large arrays, in situations where the memory savings actually matters. Does anyone care to practice that? Or its just something in the books. [Edited] Using byte

Wrapper Classes for Primitive Data Types

这一生的挚爱 提交于 2019-12-04 01:26:44
问题 In designing a solution, sometimes it may be convenient to provide wrapper classes for primitive data types. Consider a class that represents a numeric value, be it a double , a float , or an int . class Number { private: double val; public: Number(int n) : val(n) { } Number(float n) : val(n) { } Number(double n) : val(n) { } // Assume copy constructors and assignment operators exist Number& add(const Number& other) { val += other.val; return *this; } int to_int() const { return (int) val; }

Generic type checking

与世无争的帅哥 提交于 2019-12-03 18:18:16
问题 Is there a way to enforce/limit the types that are passed to primitives? (bool, int, string, etc.) Now, I know you can limit the generic type parameter to a type or interface implementation via the where clause. However, this doesn't fit the bill for primitives (AFAIK) because they do not all have a common ground (apart from object before someone says! :P). So, my current thoughts are to just grit my teeth and do a big switch statement and throw an ArgumentException on failure.. EDIT 1: Just

Cross-platform primitive data types in C++

穿精又带淫゛_ 提交于 2019-12-03 12:29:53
Unlike Java or C#, primitive data types in C++ can vary in size depending on the platform. For example, int is not guaranteed to be a 32-bit integer. Various compiler environments define data types such as uint32 or dword for this purpose, but there seems to be no standard include file for fixed-size data types. What is the recommended method to achieve maximum portability? I found this header particularly useful: BOOST cstdint Usually better than inventing own wheel (which incurs the maintenance and testing). Create a header file called types.h, and define all the fixed-size primitive types

Fastest way to check if a byte array is all zeros

心不动则不痛 提交于 2019-12-03 10:29:14
问题 I have a byte[4096] and was wondering what the fastest way is to check if all values are zero? Is there any way faster than doing: byte[] b = new byte[4096]; b[4095] = 1; for(int i=0;i<b.length;i++) if(b[i] != 0) return false; // Not Empty 回答1: I have rewritten this answer as I was first summing all bytes, this is however incorrect as Java has signed bytes, hence I need to or. Also I have changed the JVM warmup to be correct now. Your best bet really is to simply loop over all values. I

What is the use of passing const references to primitive types?

浪子不回头ぞ 提交于 2019-12-03 09:51:09
问题 In a project I maintain, I see a lot of code like this for simple get / set methods const int & MyClass::getFoo() { return m_foo; } void MyClass::setFoo(const int & foo) { m_foo = foo; } What is the point in doing that instead of the following? int MyClass::getFoo() { return m_foo; } // Removed 'const' and '&' void MyClass::setFoo(const int foo) { m_foo = foo; } // Removed '&' Passing a reference to a primitive type should require the same (or more) effort as passing the type's value itself,

Passing primitives to an OCMock's stub

℡╲_俬逩灬. 提交于 2019-12-03 09:26:28
I'm learning how to use OCMock to test my iPhone's project and I have this scenario: a HeightMap class with a getHeightAtX:andY: method, and a Render class using HeightMap . I'm trying to unit test Render using some HeightMap mocks. This works: id mock = [OCMockObject mockForClass:[Chunk class]]; int h = 0; [[[mock stub] andReturnValue:OCMOCK_VALUE(h)] getHeightAtX:0 andY:0]; Of course, works only for x=0 and y=0 . I want to test using a "flat" height map. This means I need to do something like this: id chunk = [OCMockObject mockForClass:[Chunk class]]; int h = 0; [[[chunk stub] andReturnValue

Why does `“foo”.bar = 42;` throw `TypeError` in strict mode in ES6?

社会主义新天地 提交于 2019-12-03 08:21:58
问题 According to the ES5.1 spec, the program "use strict;" "foo".bar = 42; causes a String object to be created, assigns to a property on it, and then throws the object away, resulting in no observable effects - including any exceptions. (The absence of effect can be confirmed by trying it in an ES5-compatible JS implementation like that in Opera 12.) In modern JS implementations, it throws a TypeError instead—try it: "use strict"; "foo".bar = 42; I am pretty sure the new behaviour is mandated by

In C# are the terms “Primitive” and “Literal” interchangeable?

試著忘記壹切 提交于 2019-12-03 05:53:29
A discussion earlier today led me to question whether or not my understanding of primtives and literals is correct. My understanding is that a literal type is specifically a type which can have a value assigned using a notation that both human and compiler can understand without specific type declarations: var firstName = "John"; // "John" is literal var firstName = (string)"John"; // *if* the compiler didn't understand that "John" // was a literal representation of a string then I // would have to direct it as such My understanding of primitives is that they are essentially the elemental