static-cast

Can static_cast to same type introduce runtime overhead?

允我心安 提交于 2019-12-23 07:24:31
问题 I have a structure template that takes two types ( T and S ), and at some point uses a static_cast to convert from one type to the other. It is often the case that T and S are the same type. A simplified example of the setup: template <typename T, typename S = T> struct foo { void bar(T val) { /* ... */ some_other_function(static_cast<S>(val)); /* ... */ } }; In the case that S is the same class as T , does or can the static_cast introduce extra overhead, or is it a null operation which will

Why is static_cast on an expression acting distributively?

扶醉桌前 提交于 2019-12-22 05:06:49
问题 I need to take 2 unsigned 8-bit values and subtract them, then add this value to a 32-bit accumulator. The 8-bit subtraction may underflow, and that's ok (unsigned int underflow is defined behavior, so no problems there). I would expect that static_cast<uint32_t>(foo - bar) should do what I want (where foo and bar are both uint8_t ). But it would appear that this casts them first and then performs a 32-bit subtraction, whereas I need it to underflow as an 8-bit variable. I know I could just

Is static_cast misused?

纵然是瞬间 提交于 2019-12-22 01:53:54
问题 I have mixed feelings about static_cast , as it is the safest C++ cast available, but allows both safe and unsafe conversions at the same time, so you have to know the context to say if it is actually safe or might lead to UB (e.g. when casting to a sub-class). So why isn't there a safer explicit cast? Here is an example, where it could be useful. In COM, they have to return the interface pointer as void** ppv , so "have to" cast explicitely *ppv = (IInterface*) this; which was then suggested

Why is `decltype(static_cast<T>(…))` not always `T`?

被刻印的时光 ゝ 提交于 2019-12-20 16:49:09
问题 For the following code, all but the last assertion passes: template<typename T> constexpr void assert_static_cast_identity() { using T_cast = decltype(static_cast<T>(std::declval<T>())); static_assert(std::is_same_v<T_cast, T>); } int main() { assert_static_cast_identity<int>(); assert_static_cast_identity<int&>(); assert_static_cast_identity<int&&>(); // assert_static_cast_identity<int(int)>(); // illegal cast assert_static_cast_identity<int (&)(int)>(); assert_static_cast_identity<int (&&)

What is the static_cast runtime overhead if adding constness while keeping the same type?

本秂侑毒 提交于 2019-12-20 04:27:11
问题 I find it irritating that I can call non-const functions of an object if I have a pointer to this object. I cannot let the pointer be a const pointer because there are also non-const functions I need to call. Therefore, my only option seems to do static_casts to ensure that constness also works across pointers. Here is a minimal example: class MyClassImpl { MyClassImpl(void) : m_i(0) {} int increment(void) { ++m_i; return m_i; } private: int m_i; }; class MyClass { MyClass(void) : m_pImpl(new

reinterpret_cast error for enum

那年仲夏 提交于 2019-12-19 16:36:25
问题 Why i can't use reinterpret_cast operator for such a cast? enum Foo { bar, baz }; void foo(Foo) { } int main() { // foo(0); // error: invalid conversion from 'int' to 'Foo' // foo(reinterpret_cast<Foo>(0)); // error: invalid cast from type 'int' to type 'Foo' foo(static_cast<Foo>(0)); foo((Foo)0); } 回答1: I think that reinterpret_cast can be use for all types of casts, because it's force any type casts to another type with all side-effects of this conversion. That is a common misconception.

When is static cast safe when you are using multiple inheritance?

家住魔仙堡 提交于 2019-12-17 19:34:38
问题 I found myself in a situation where I know what type something is. The Type is one of three (or more) levels of inheritance. I call factory which returns B* however T is either the highest level of a type (if my code knows what it is) or the 2nd level. Anyways, I did a static_cast in the template which is the wrong thing to do. My question is WHEN can I static cast safely? Is there ever such a time? I did it in this case because I'd rather get compile errors when I accidentally have T as

Proper way of casting pointer types

牧云@^-^@ 提交于 2019-12-17 17:39:15
问题 Considering the following code (and the fact that VirtualAlloc() returns a void*): BYTE* pbNext = reinterpret_cast<BYTE*>( VirtualAlloc(NULL, cbAlloc, MEM_COMMIT, PAGE_READWRITE)); why is reinterpret_cast chosen instead of static_cast ? I used to think that reinterpret_cast is OK for e.g. casting pointers to and from integer types (like e.g. DWORD_PTR ), but to cast from a void* to a BYTE* , isn't static_cast OK? Are there any (subtle?) differences in this particular case, or are they just

Proper way of casting pointer types

你说的曾经没有我的故事 提交于 2019-12-17 17:39:09
问题 Considering the following code (and the fact that VirtualAlloc() returns a void*): BYTE* pbNext = reinterpret_cast<BYTE*>( VirtualAlloc(NULL, cbAlloc, MEM_COMMIT, PAGE_READWRITE)); why is reinterpret_cast chosen instead of static_cast ? I used to think that reinterpret_cast is OK for e.g. casting pointers to and from integer types (like e.g. DWORD_PTR ), but to cast from a void* to a BYTE* , isn't static_cast OK? Are there any (subtle?) differences in this particular case, or are they just

What is the difference between static_cast and Implicit_cast?

一笑奈何 提交于 2019-12-17 15:46:22
问题 What is implicit_cast? when should I prefer implicit_cast rather than static_cast? 回答1: I'm copying over from a comment i made to answer this comment at another place. You can down-cast with static_cast . Not so with implicit_cast . static_cast basically allows you to do any implicit conversion, and in addition the reverse of any implicit conversion (up to some limits. you can't downcast if there is a virtual base-class involved). But implicit_cast will only accept implicit conversions. no