static-cast

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

两盒软妹~` 提交于 2019-12-02 05:58:58
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 MyClassImpl()){} ~MyClass(void) { delete m_pImpl; } int doNothing(void) const { m_pImpl->increment();

C++, statically detect base classes with differing addresses?

雨燕双飞 提交于 2019-12-02 02:26:20
If I have a derived class with multiple bases, each this pointer for each base will be different from that of the derived object's this pointer, except for one. Given two types in an inheritance hierarchy, I'd like to detect at compile time whether they share the same this pointer. Something like this should work, but doesn't: BOOST_STATIC_ASSERT(static_cast<Base1*>((Derived *)0xDEADBEEF) == (Derived*)0xDEADBEEF); Because it needs to be an 'integral constant expression' and only integer casts are allowed in those according to the standard (which is stupid, because they only need compile time

C++, statically detect base classes with differing addresses?

我怕爱的太早我们不能终老 提交于 2019-12-02 01:42:44
问题 If I have a derived class with multiple bases, each this pointer for each base will be different from that of the derived object's this pointer, except for one. Given two types in an inheritance hierarchy, I'd like to detect at compile time whether they share the same this pointer. Something like this should work, but doesn't: BOOST_STATIC_ASSERT(static_cast<Base1*>((Derived *)0xDEADBEEF) == (Derived*)0xDEADBEEF); Because it needs to be an 'integral constant expression' and only integer casts

casting to void* to pass objects to pthread in c++

耗尽温柔 提交于 2019-12-01 20:10:00
I'm a little confused about how to pass an object to the pthread_create function. I've found a lot of piecemeal information concerning casting to void*, passing arguments to pthread_create, etc., but nothing that ties it all together. I just want to make sure I've tied it all together and am not doing anything stupid. Let's say I have the following thread class: Edit: fixed mis-matched static_cast . class ProducerThread { pthread_t thread; pthread_attr_t thread_attr; ProducerThread(const ProducerThread& x); ProducerThread& operator= (const ProducerThread& x); virtual void *thread_routine(void

reinterpret_cast for almost pod data (is layout-compatibility enough)

柔情痞子 提交于 2019-12-01 16:18:02
I am trying to learn about static_cast and reinterpret_cast . If I am correct the standard (9.2.18) says that reinterpret_cast for pod data is safe: A pointer to a POD-struct object, suitably converted using a reinterpret_cast , points to its initial member (or if that member is a bit-field, then to the unit in which it resides) and vice versa. [ Note: There might therefore be unnamed padding within a POD-struct object, but not at its beginning, as necessary to achieve appropriate alignment. — end note ] My question is how strictly to interpret this. Is, for example, layout-compatibility

static_cast and reinterpret_cast for std::aligned_storage

血红的双手。 提交于 2019-12-01 15:29:33
could someone please explain the bit of code about casting in http://en.cppreference.com/w/cpp/types/aligned_storage please? can the following code return *static_cast<const T*>(static_cast<const void*>(&data[pos])); be replaced with return *reinterpret_cast<const T*>(&data[pos]); ? Why here two casting are used? Thanks a lot. Hong According to the standard (§ 5.2.10 reinterpret_cast , section 7): A pointer to an object can be explicitly converted to a pointer to a different object type. When a prvalue v of type “pointer to T1 ” is converted to the type “pointer to cv T2 ”, the result is static

reinterpret_cast for almost pod data (is layout-compatibility enough)

可紊 提交于 2019-12-01 15:17:57
问题 I am trying to learn about static_cast and reinterpret_cast . If I am correct the standard (9.2.18) says that reinterpret_cast for pod data is safe: A pointer to a POD-struct object, suitably converted using a reinterpret_cast , points to its initial member (or if that member is a bit-field, then to the unit in which it resides) and vice versa. [ Note: There might therefore be unnamed padding within a POD-struct object, but not at its beginning, as necessary to achieve appropriate alignment.

static_cast and reinterpret_cast for std::aligned_storage

时光怂恿深爱的人放手 提交于 2019-12-01 14:24:49
问题 could someone please explain the bit of code about casting in http://en.cppreference.com/w/cpp/types/aligned_storage please? can the following code return *static_cast<const T*>(static_cast<const void*>(&data[pos])); be replaced with return *reinterpret_cast<const T*>(&data[pos]); ? Why here two casting are used? Thanks a lot. Hong 回答1: According to the standard (§ 5.2.10 reinterpret_cast , section 7): A pointer to an object can be explicitly converted to a pointer to a different object type.

static_cast'd pointer value

孤街醉人 提交于 2019-12-01 03:33:26
In the current draft standard (and C++17), this is written about static_casting a void * : A prvalue of type “pointer to cv1 void” can be converted to a prvalue of type “pointer to cv2 T”, where T is an object type and cv2 is the same cv-qualification as, or greater cv-qualification than, cv1 . If the original pointer value represents the address A of a byte in memory and A does not satisfy the alignment requirement of T, then the resulting pointer value is unspecified. Otherwise, if the original pointer value points to an object a , and there is an object b of type T (ignoring cv

static_cast'd pointer value

喜夏-厌秋 提交于 2019-12-01 00:54:51
问题 In the current draft standard (and C++17), this is written about static_casting a void * : A prvalue of type “pointer to cv1 void” can be converted to a prvalue of type “pointer to cv2 T”, where T is an object type and cv2 is the same cv-qualification as, or greater cv-qualification than, cv1 . If the original pointer value represents the address A of a byte in memory and A does not satisfy the alignment requirement of T, then the resulting pointer value is unspecified. Otherwise, if the