Questions tagged [static-cast]

A C++ cast operator to convert from one type to another, using only information about the static type of the object being cast

From cppreference.com:

Converts between types using a combination of implicit and user-defined conversions. It has the form static_cast<new_type>(expression).

static_cast is the C++ cast operator used to convert from one type to another. It only uses information about the static type of the object being cast and not its dynamic type; i.e. using only information known at compile-time and not performing a run-time check. The conversion returns a value of type being converted to.

As with all cast expressions, the result is:

  • an lvalue if new_type is an lvalue reference type or an rvalue reference to function type;
  • an xvalue if new_type is an rvalue reference to object type;
  • a prvalue otherwise.
507 questions
0
votes
0 answers

static_cast of "this" to another base class

Is the following use of static_cast in a constructor "safe"? For context I'm experimenting with a Win32 window wrapper using a policy-based design. Part of it involves creating a dispatcher class by inheriting from a base dispatcher that holds a…
Robinson
  • 9,666
  • 16
  • 71
  • 115
0
votes
2 answers

C++, Is it safe to static_cast to void* and back across processes?

I have two identical binaries running at the same time on a Linux system (different command line arguments, so they are doing different things). In one process, I memcopy an object into some shared memory region; and in another process, I retrieve…
MetroWind
  • 541
  • 4
  • 16
0
votes
0 answers

Why I cannot `static_cast` from `const char *` to `const PVOID` (`typedef void *`)?

But static_cast from const char * to CPVOID, which is typedef const void *, is seamless. Is there some difference between const PVOID and CPVOID, that I don't understand? Code: typedef void * PVOID; typedef const void * CPVOID; int main() { …
Vizor
  • 396
  • 3
  • 14
0
votes
1 answer

How to static cast 2D vector size() (size_t to int) in C++?

I have not found any other posts which answer this question. This is a c++ program. I am receiving an error at the line: int entryval = static_cast(database[0].size()); The error says: Thread 1: EXC_BAD_ACCESS (code = 1, address = 0x8) it…
Mr Berry
  • 115
  • 10
0
votes
2 answers

When Will static_casting the Result of ceil Compromise the Result?

static_casting from a floating point to an integer simply strips the fractional point of the number. For example static_cast(13.9999999) yields 13. Not all integers are representable as floating point numbers. For example internally the closest…
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
0
votes
0 answers

clang accepts "static_cast" using "explicit operator int() const", gcc/VS reject

Given this c++11 code: (everything remains the same compiling as c++14 or c++17) class typeSafe { public: typeSafe(int tValue) : value(tValue) { } explicit operator int() const { return value; } private: int…
user1902689
  • 1,655
  • 2
  • 22
  • 33
0
votes
1 answer

Type conversion by "explicit operator X" for static_cast or toX()?

There are similar previous questions, but all the ones I could find ask different questions: static_cast vs. direct call to conversion operator - Related but different, discusses static_cast(x) vs x.operator string() Many talking about…
user1902689
  • 1,655
  • 2
  • 22
  • 33
0
votes
1 answer

Is there static_cast keyword or standard method in Python?

I got the following piece of code: def DisplayListCurveKeys(pCurve, pProperty): lKeyCount = pCurve.KeyGetCount() for lCount in range(lKeyCount): lKeyValue = static_cast(pCurve.KeyGetValue(lCount)) lKeyTime =…
Dims
  • 47,675
  • 117
  • 331
  • 600
0
votes
2 answers

Using static_cast on char* type to get the address , and getting 24 bit and 48-bit address

#include using std::cout; using std::endl; int main() { char name[] = "abcde"; //char *name = "abcde"; cout << name<<"\n"; //type casting to get the address of char array cout<(name); return…
N. Raj
  • 319
  • 4
  • 14
0
votes
4 answers

C-style cast of std::vector

I stumbled upon this implementation in an existing code base when trying to find a solution to casting an std::vector to a std::vector. I am using C++11. Consider the following code snippet: #include #include…
antogilbert
  • 176
  • 2
  • 12
0
votes
3 answers

Error converting types in C++

I have a program in which I need to use the Format(); function to combine a string literal and a int into a CString variable. I have tried several different ways of doing this, the code for them is here: // declare variables used CString…
Nate Koppenhaver
  • 1,676
  • 3
  • 21
  • 31
0
votes
1 answer

Converting uint64_t to void* and back

I am trying the following approach to convert a handle to void* and then back to the handle the following way uint64_t hInt = 154071804376; //assume this is a valid memory location void* hPoint = reinterpret_cast(hInt); uint64_t hIntBack =…
user3294816
  • 37
  • 1
  • 7
0
votes
2 answers

Use static cast because dynamic cast fails. Bad practice?

On a project I am working, I noticed that a dynamic downcast fails. A quick code examination confirmed that the particular object was actually never of that type. However, I saw that other developers enforce this very same cast by applying a static…
Adrian Schneider
  • 1,427
  • 1
  • 11
  • 17
0
votes
0 answers

Why does static cast eat reference?

Consider the following code: struct base {}; struct derived : public base {}; int main() { derived d = {}; derived* ptr_d = &d; derived*& ref_ptr_d = ptr_d; base*& ref_ptr_b = static_cast(ref_ptr_d); // this won't compile …
ivaigult
  • 6,198
  • 5
  • 38
  • 66
0
votes
2 answers

"Cross-cast" attempt using static_cast failing; why?

Why doesn't ths "cross-cast" work? The following creates this object inheritance model: FOOD / \ CHEESE CAKE Here I attempt to static_cast a pointer from cheese to cake, making a cheesecake :D. I am getting the following error from…
kmiklas
  • 13,085
  • 22
  • 67
  • 103