Questions tagged [casting]

Casting is a process where an object type is explicitly converted into another type if the conversion is allowed.

Filter by
Sorted by
Tagged with
4
votes
0answers
78 views

Implementation of narrow_cast in C++

To summarize what I've been trying to do, I basically tried to make a safe narrow_cast operator, which casts to the Target type if and only if the value is ...
7
votes
1answer
76 views

An explicit cast for narrowing numeric conversions

Inspired by gsl::narrow_cast, I created my own implementation with the addition of a couple of features: a static assert on the types to ensure the cast is actually narrowing (if future changes to ...
2
votes
0answers
50 views

Convenient way to make an UIImageView ExpressibleByStringLiteral

I want to reduce boiler-plate code and write like this when developing in iOS: let imgView: UIImageView = "share_fareware_text_bg" Here is implementation code: <...
5
votes
1answer
53 views

Add-in for input() with validation

What do you think about this code? Do you have some advices and remarks for me about them? I have started learning python recently. ...
18
votes
5answers
2k views

Count Even Digits In Number

I was asked to complete this CodeStepByStep problem. Here's a quick summary: Write a function named count_even_digits that accepts two integers as parameters ...
0
votes
1answer
75 views

Different payment details in a common list, to be casted [closed]

I need to be able to access a number of different payment methods in a common list. They can be different methods - credit card, cheque, direct debit etc. The only common attributes are the payment ...
4
votes
0answers
46 views

Safe pointer casting : static_cast or dynamic_cast

Context: I'm using the clone pattern for creating copies of polymorphic classes which may use virtual inheritance (I hope to get rid of the latter). So when calling ...
0
votes
2answers
93 views

Servlet filter which detects if the authenticated user is a service account

My concern with this code is that I am forcing execution repeatedly through the same pathway for the sake of a readability that I'm not convinced by. It's a servlet filter which detects if the ...
5
votes
2answers
148 views

add_const or remove_const based on another type

Here is a type trait which - I feel - could fit in std next to std::add_const and ...
4
votes
1answer
62 views

Making a Zsync file archive with checksums

I'm learning go by doing it. I tried to port the Java's ZsyncMake implementation into Golang. I also employ the Go's concurrency API with goroutine and channel. I have some experience in Java, but ...
-4
votes
1answer
52 views

Embedded code for another executable - Lara walk handler decompilation [closed]

I'm writing an independent code which I'll import into an exe, Here is an self-contained example: This code is a PCSTATE handler - basically when lara starts walking - this code will be executed. It ...
0
votes
1answer
71 views

Casting the left and right numbers as integers or doubles

How can I remove the code duplication in this code? It is trying to cast the left and right numbers as integers or doubles and then using it. ...
3
votes
2answers
885 views

Writing a generic casting extension-method

I'm writing a C# extension-method that converts any object to a type. E.g. the method shall convert a string with value "y" to a ...
8
votes
3answers
711 views

'StrictInt' Python object class that prohibits casting numbers with non-integer components to `int`

Inspired by and created because of python3 utility: convert safely to int and driven partly by the 'refactor' in my answer. The aforementioned question and my answer drove me to go and do a thing ...
2
votes
2answers
173 views

Simplifying casting in my C/C++ qsort char** comparator function

I have a comparator function I use with qsort to sort an array of char* (i.e., char**), ...
4
votes
1answer
12k views

Workaround for unchecked cast of a deserialized Object to ArrayList<Vehicle>

For a class I was assigned to write code to read objects of the class Vehicle using ObjectInputStream (in). The objects are ...
6
votes
3answers
926 views

Converting Array of Floats to UINT8 (`char`) or UINT16 (`unsigned short`) Using SSE4

The problem is given image in 32 Bit Floating Point Format (float) how to convert it to UINT8 (char) or UNIT16 (...
3
votes
1answer
61 views

Trait for specifying a type parameter for Into::into

There are occasionally times when it would be nice to be able to specify a type parameter for Into::into. This doesn't happen very often—mostly just when you ...
0
votes
1answer
74 views

Java inheritance and generics [closed]

Today i had discussion with my colleague about casting. Our case: We have root class ...
4
votes
3answers
123 views

Associating a string with a derived class

I am using the following code to get a string that is associated with the class of an instance: ...
4
votes
1answer
75 views

Convertible templated math vector

I've made a templated math vector struct with a templated type and templated dimension count. I want my vectors to be convertible so I can easily make, for example, a ...
9
votes
6answers
2k views

Rules engine dynamic type casting

I have a basic switch statement that executes some code on expectedValue and givenValue. The code to execute is guided by the ...
6
votes
1answer
545 views

Acyclic Visitor Pattern without dynamic_cast

The goal is to minimize boilerplate code for creating visitors while allowing users to easily define custom visitors without having to add new accept methods for each visitor type. For example, a ...
2
votes
1answer
395 views

WGS84-coordinate encoded as Integers

I want to store WGS84-coordinates in two integers for latitude and longitude instead of floats to get a higher precision than with floats. Here's my code: ...
2
votes
1answer
144 views

check for null before or after casting

I have a list of BasicDBObject that I fetch from my database. This is basically a JSON document converted into a java Object. Let's assume that we have a ...
7
votes
1answer
143 views

python3 utility: convert safely to int

I made this utility function to check for parameters. All it does is checking whether a given variable is an int, or a float/str that represent exactly an int (ie. no decimal part or NaN), and casting ...
1
vote
1answer
112 views

Testing whether an abstract object is an instance of one of the several concrete classes

I have an abstract object and I need to test whether it is an instance of one of the several concrete classes. Every concrete class has a Value property that is not ...
11
votes
4answers
9k views

Convert a boxed integer to an nullable integer with potentially different type

Given a boxed integer (with a boxed short, int, ...) I want to convert it to an unboxed, nullable type instead. It should be ...
4
votes
1answer
136 views

Test and implementation of a reorderable data structure that stores number of users (classifiable as per gender, country, etc.) of an Application

I have implemented a data structure that allows storage of number of users of a hypothetical application in a tree-like data structure in nodes as per their classification based on categories such as ...
1
vote
0answers
105 views

Helper class for accessing blobs and mmap memory - follow up

This class was inspired from Microsoft's array_view. It intended to use with mmap-ed memory for easily check bounds and so on. I probably will need to throw exceptions, but this is not my usual way ...
2
votes
1answer
152 views

Helper class for accessing blobs and mmap memory

This class was inspired from Microsoft's array_view. It intended to use with mmap-ed memory for easily check bounds and so on. ...
2
votes
2answers
9k views

Alternative to using Object and casting in a map

I have the following class which represents a set of properties. ...
4
votes
2answers
159 views

DB-to-Java value mapper

In my company, I've inherited some Java library that I'm now writing tests to, refactoring and fixing Sonar issues. One particular point that Sonar is complaining about is a big chaining of ...
3
votes
1answer
134 views

Constrained type alias

What do you think about the following syntax for some very simple and intensively reusable validations? Does this: string name = (SomeText)"Hm…"; mean for you ...
2
votes
1answer
169 views

Byte conversion in my Caesar's cipher

I was wondering if I am doing any useless conversions byte←→int, for example: byte((int(ch-'A')+shift)%26 + 'A') Converting ...
4
votes
1answer
2k views

Normalize integer types to float range

I wrote two template functions to take an integer type and output a float in the given range. One function takes signed integers and the other takes unsigned ...
3
votes
1answer
2k views

Setting various types of properties on a JCR node

This code checks value's type and cast it into appropriate type, then puts it into a javax.jcr.Node. The checking looks long and boring. Is there a way to refactor ...
2
votes
1answer
99 views

Program to count number of victories in a pool of contests

I changed the program to use a dynamic array instead of a vector or storing the integers. This program works without bugs and executes relatively fast. The only doubt Istill have is how to fill the ...
5
votes
3answers
2k views

Comparing Equals() method from MSDN

I've implemented the Equals() support for my class as follows: ...
4
votes
2answers
83 views

Setting the positions of nodes based on a layout

I have a bit of code that looks like this: ...
3
votes
1answer
64 views

Implicit type class for updating maps

With the help of SO I got my code working, but I think it still could be improved, in particular I do not like using asInstanceOf. Is there some way how to avoid it?...
4
votes
1answer
230 views

strncpy To strcpy Equivalence

I have this ugly function, and I feel that the entire strncpy should just be an strcpy: ...
2
votes
1answer
50 views

Treating a short like an uchar[2]

I need to create a commandword to be send to hardware and I'd like to make is as efficient as possible (without going to Assembler or similar). The command is 16Bit long and has to be passed as an ...
3
votes
1answer
1k views

Sorting polymorphic classes

I'm learning from "Jumping into C++" by Alex Allain (sample chapter and TOC) and solved the first problem in Chapter 26 "Inheritance and Polymorphism". I'm especially concerned about my use of ...
4
votes
1answer
721 views

A less verbose const_cast

Motivation const_cast is a beast that I seldomly encounter. But when I do so it is mostly a bad experience. Nobody knows what most of the C++ programmers do but ...
7
votes
1answer
488 views

Efficient generic type conversion between numeric types in F#

It's easy to write a function that adds two ints in F#: let add x y = x + y Actually, it's the same as: ...
5
votes
2answers
61 views

Getting a set of subscribers from a subscriberMap

In my Java event bus project, I have a private generic method that retrieves a Set<Subscriber<E>> from a private ...
1
vote
1answer
7k views

Casting between types for shared_ptr

I'm implementing an Entity-Component-System. Basically, an entity is a wrapper around an ID, a component is just a POD struct, and a System is a class that does work on components corresponding to the ...
10
votes
1answer
213 views

Here's my interface, so call me …maybe

I'm inspecting VBA code to find obsolete code constructs - namely explicit Call statements, like this: Call DoSomething(42) ...
5
votes
5answers
13k views

Performance of object-to-string conversion

Using an OR/M I map a lot of rows from database to array of objects (~300k). In these objects some properties are marked with special attribute [Signed]. Row-by-row ...