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
1 vote
1 answer
44 views

safe numeric type converter

I have written a function for casting between built in numeric types. I built it to check if the source value is within the range of the destination value, and to provide a nicely legible error if not....
Diederick C. Niehorster's user avatar
2 votes
1 answer
80 views

Absolute value meta programming

I am implementing a generic absolute value function that handles signed and unsigned integer values correctly over the input type's domain. The std::abs(...) ...
dvnlo's user avatar
  • 135
5 votes
3 answers
378 views

Templatizing a Timer class

I'm making a timer class in C++. This is the overview: ...
Alex CB's user avatar
  • 175
1 vote
1 answer
84 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, ...
Aganju's user avatar
  • 189
3 votes
1 answer
96 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 ...
Simon Richter's user avatar
0 votes
1 answer
68 views

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

have the following struct with one method: ...
DragonGamer's user avatar
4 votes
2 answers
821 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 ...
TimeToCode's user avatar
1 vote
1 answer
150 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 ...
Ryan Peschel's user avatar
2 votes
1 answer
353 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 ...
Dmitry's user avatar
  • 121
2 votes
3 answers
268 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 ...
JimmyHu's user avatar
  • 3,918
0 votes
1 answer
612 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,...
Jonathan Woollett-light's user avatar
1 vote
2 answers
6k 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 ...
Juneyoung Oh's user avatar
3 votes
3 answers
231 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'...
CL40's user avatar
  • 133
2 votes
1 answer
73 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: ...
Minh Tran's user avatar
  • 285
6 votes
1 answer
2k 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 ...
Famiu's user avatar
  • 196
10 votes
3 answers
710 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 ...
John Ilacqua's user avatar
2 votes
1 answer
149 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: <...
black_pearl's user avatar
5 votes
1 answer
64 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. ...
Owl's user avatar
  • 204
20 votes
5 answers
7k 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 ...
Linny's user avatar
  • 9,948
0 votes
1 answer
87 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 ...
JimboFlex's user avatar
  • 111
4 votes
0 answers
77 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 ...
Flamefire's user avatar
  • 241
0 votes
2 answers
716 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 ...
Adam's user avatar
  • 139
5 votes
2 answers
581 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 ...
einpoklum's user avatar
  • 1,985
4 votes
1 answer
103 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 ...
imeluntuk's user avatar
  • 223
-4 votes
1 answer
184 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 ...
FISOCPP's user avatar
  • 93
0 votes
1 answer
75 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. ...
WAQT's 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 ...
Matthias Burger's user avatar
8 votes
3 answers
992 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 ...
Thomas Ward's user avatar
  • 2,518
2 votes
2 answers
287 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**), ...
Alex Reynolds's user avatar
8 votes
1 answer
20k 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 ...
brienna's user avatar
  • 243
7 votes
3 answers
3k 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 (...
Royi's user avatar
  • 582
3 votes
1 answer
67 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 ...
kardeiz's user avatar
  • 571
0 votes
1 answer
79 views

Java inheritance and generics [closed]

Today i had discussion with my colleague about casting. Our case: We have root class ...
regisik's user avatar
4 votes
3 answers
143 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: ...
oddRaven's user avatar
  • 157
4 votes
1 answer
98 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 ...
Willy Goat's 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 ...
IbrarMumtaz's user avatar
6 votes
1 answer
891 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 ...
Lars Melchior's user avatar
2 votes
1 answer
680 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: ...
user2033412's user avatar
2 votes
1 answer
522 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 ...
felix's user avatar
  • 588
7 votes
1 answer
186 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 ...
Zorgatone's user avatar
  • 436
1 vote
1 answer
116 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 ...
oddRaven's user avatar
  • 157
12 votes
5 answers
14k 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 ...
Micha Wiedenmann's user avatar
4 votes
1 answer
146 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 ...
Dhruva Bharadwaj's user avatar
1 vote
0 answers
119 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 ...
Nick's user avatar
  • 1,490
2 votes
1 answer
190 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. ...
Nick's user avatar
  • 1,490
2 votes
2 answers
14k views

Alternative to using Object and casting in a map

I have the following class which represents a set of properties. ...
ksl's user avatar
  • 141
4 votes
2 answers
173 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 ...
Rafael Eyng's user avatar
3 votes
1 answer
143 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 ...
Dmitry Nogin's user avatar
  • 6,113
3 votes
1 answer
273 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 ...
Jorge Bucaran's user avatar
5 votes
1 answer
3k 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 ...
Chase's user avatar
  • 273