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
5 votes
3 answers
299 views

Templatizing a Timer class

I'm making a timer class in C++. This is the overview: ...
user avatar
  • 75
1 vote
1 answer
47 views

Verified downcasting if RTTI is active

In an application that uses heavily polymorphic classes, I have often the need to downcast a pointer or a reference to the correct derived class. They should always be of the correct derived class, ...
user avatar
  • 188
3 votes
1 answer
46 views

checked conversion function for C++20

Similar to C++ int_cast<> function for checked casts?, but C++20, and with target type deduced from context. The goal is to implement a runtime check that the value being converted can be ...
user avatar
0 votes
1 answer
66 views

How to not clutter intended casts with static_cast<>? [closed]

have the following struct with one method: ...
user avatar
4 votes
2 answers
809 views

Method for create a copy of List<T>

I created a method to return a copy of List<T>, basically I convert the List<T> into an array ...
user avatar
1 vote
1 answer
73 views

Checking if two shapes collide in TypeScript (double dispatch)

Basically, I'm trying to create a generic way to check if two shapes are colliding. I think this is kind of a "double-dispatch" problem, but I'm unsure if there is a better way to solve it ...
user avatar
2 votes
1 answer
96 views

Safe runtime numeric casts

The rationale behind this code is to implement a runtime-safe number conversions in situations when precision loss is possible, but is not expected. Example: passing a ...
user avatar
  • 121
2 votes
3 answers
257 views

ArrayDowncasters Implementation for Downcasting from System.Array to Array of Specific Type in C#

I am working with System.Array and I am trying to convert System.Array objects to array of specific type (such as ...
user avatar
  • 3,556
0 votes
1 answer
270 views

Converting from `std::array<std::variant<>>` to `void*` [closed]

I'm doing some work with Vulkan, in this I need to pass a set of values of various types. Currently I'm using std::array<std::variant<>> in my interface,...
user avatar
1 vote
2 answers
3k views

How to make Map<String, Object> expendible(?) safely in java

I am building some statistic counter in Java. Each metric could have flexible depth so the implementation should able to explore the depth when value insert or update. So I implement like below (with ...
user avatar
3 votes
3 answers
140 views

Returning interfaces in Java without significant casting

I am writing some code that takes and returns interfaces following the patterns in Effective Java Third Edition. I understand the principle - we want to take in interfaces because in many cases we don'...
user avatar
  • 133
2 votes
1 answer
64 views

Parsing csv tokens as doubles and identifying which threw FormatException

Problem Statement I'm reading each line from a .csv file and parsing each comma-delimited value and casting it to the appropriate type: ...
user avatar
  • 285
6 votes
1 answer
1k 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 ...
user avatar
  • 196
8 votes
2 answers
463 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 ...
user avatar
2 votes
0 answers
104 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: <...
user avatar
5 votes
1 answer
59 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. ...
user avatar
  • 204
20 votes
5 answers
6k 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 ...
user avatar
  • 9,587
0 votes
1 answer
82 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 ...
user avatar
  • 111
4 votes
0 answers
66 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 ...
user avatar
  • 241
0 votes
2 answers
581 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 ...
user avatar
  • 139
5 votes
2 answers
418 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 ...
user avatar
  • 1,925
4 votes
1 answer
96 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 ...
user avatar
  • 223
-4 votes
1 answer
56 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 ...
user avatar
  • 93
0 votes
1 answer
72 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. ...
user avatar
  • 117
3 votes
2 answers
3k 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 ...
user avatar
8 votes
3 answers
825 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 ...
user avatar
  • 2,498
2 votes
2 answers
243 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**), ...
user avatar
8 votes
1 answer
18k 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 ...
user avatar
  • 243
7 votes
3 answers
2k 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 (...
user avatar
  • 562
3 votes
1 answer
65 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 ...
user avatar
  • 561
0 votes
1 answer
77 views

Java inheritance and generics [closed]

Today i had discussion with my colleague about casting. Our case: We have root class ...
user avatar
4 votes
3 answers
131 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: ...
user avatar
  • 157
4 votes
1 answer
93 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 ...
user avatar
9 votes
6 answers
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 ...
user avatar
6 votes
1 answer
809 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 ...
user avatar
2 votes
1 answer
614 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: ...
user avatar
2 votes
1 answer
432 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 ...
user avatar
  • 588
7 votes
1 answer
170 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 ...
user avatar
  • 436
1 vote
1 answer
113 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 ...
user avatar
  • 157
12 votes
5 answers
13k 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 ...
user avatar
4 votes
1 answer
141 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 ...
user avatar
1 vote
0 answers
112 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 ...
user avatar
  • 1,266
2 votes
1 answer
183 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. ...
user avatar
  • 1,266
2 votes
2 answers
13k views

Alternative to using Object and casting in a map

I have the following class which represents a set of properties. ...
user avatar
  • 141
4 votes
2 answers
169 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 ...
user avatar
3 votes
1 answer
140 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 ...
user avatar
  • 6,123
3 votes
1 answer
228 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 ...
user avatar
5 votes
1 answer
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 ...
user avatar
  • 273
3 votes
1 answer
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 ...
user avatar
2 votes
1 answer
110 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 ...
user avatar
  • 207