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
79 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 ...
2
votes
2answers
1k views

Version of lexical_cast that doesn't use exceptions

I love boost::lexical_cast but its usage of exceptions there is not prefered in most situations I use it. I have implemented a version and it seems to work but I ...
7
votes
1answer
77 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
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 ...
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 ...
13
votes
2answers
12k views

C++ string_cast<> template function

In C++, to simplify string conversion between std::string and std::wstring, I created the following utility template functions: ...
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 ...
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 ...
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
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
895 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 ...
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 ...
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**), ...
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 ...
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
928 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 (...
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 ...
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 ...
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 ...
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: ...
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 ...
4
votes
1answer
137 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 ...
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 ...
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 ...
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
153 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. ...
9
votes
5answers
5k views

Python odd number diamond

I worked up a quick diamond program that takes only odd numbers while returning an error with even numbers or 0. The code looks rather ugly to me, especially with the recurring ...
24
votes
2answers
53k views

TryCast<T> method

This isn't urgent, it is more along the lines of trivia or a challenge. The solution works fine as it is, but I suspect it could be better. What follows is a method I came up with a while back in a ...
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. ...
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
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 ...
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 ...
4
votes
1answer
174 views

CRAIICall class

I thought it was a good idea to use this in my C++ projects: ...
4
votes
4answers
461 views

Euclidian distance - optimization and casting

I'm trying to optimize a simple Euclidian distance function in C. It's for a DLL, and calculates distances from one point to many. I have: Version 1: ...
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: ...
6
votes
2answers
2k views

Converting a region code (short string up to 4 characters) to a 32-bit integer

There is a function that converts region code strings (1 to 4 characters and null terminator) to 32 bit integers codes to be used in maps as keys or values. Blindly casting char* to int* is bad as it ...
4
votes
2answers
83 views

Setting the positions of nodes based on a layout

I have a bit of code that looks like this: ...
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: ...
3
votes
3answers
217 views

Limited typecasting with regex

I'm learning Python, and found a fun little video on YouTube called "Learn Python through public data hacking". In essence it uses the CTA unofficial API to do some parsing of XML data. In taking ...