static-cast

CRTP: How to infer type of member to be used as return type?

感情迁移 提交于 2019-12-12 18:27:47
问题 I would like to make the return type of a CRTP base method depend on the type of a member in the derived, as for example in: template <typename C> struct sum_a_b { ??? sum() { return static_cast<C*>(this)->a + static_cast<C*>(this)->b; } } template <typename T> struct a_b : sum_a_b<a_b<T>> { T a,b; }; What should I put in place of ??? I tried different ways to declare the return type : template <typename T> struct base { int get_ok() { return static_cast<T*>(this)->value; } auto get_invalid()

Accessing subclass members from a superclass pointer C++

孤街浪徒 提交于 2019-12-12 08:13:56
问题 I have an array of custom class Student objects. CourseStudent and ResearchStudent both inherit from Student, and all the instances of Student are one or the other of these. I have a function to go through the array, determine the subtype of each Student, then call subtype-specific member functions on them. The problem is, because these functions are not overloaded, they are not found in Student, so the compiler kicks up a fuss. If I have a pointer to Student, is there a way to get a pointer

Why can I use static_cast With void* but not With char*

独自空忆成欢 提交于 2019-12-12 06:34:09
问题 I know that reinterpret_cast is primarily used going to or from a char* . But I was surprised to find that static_cast could do the same with a void* . For example: auto foo "hello world"s; auto temp = static_cast<void*>(&foo); auto bar = static_cast<string*>(temp); What do we gain from using reinterpret_cast and char* over static_cast and void* ? Is it something to do with the strict aliasing problem? 回答1: Generally speaking, static_cast will do cast any two types if one of them can be cast

Print an address of function in C++, g++/clang++ vs vc++ , who is right?

帅比萌擦擦* 提交于 2019-12-11 08:54:53
问题 Consider following simple program: #include <iostream> void foo() { } int main() { std::cout<<static_cast<void*>(foo); } It compiles fine on VC++ but g++ & clang++ gives compilation errors. See live demo here ( VC++ ) See live demo here ( clang++ ) See live demo here ( g++ ) Diagnostics given by g++ & clang++ : source_file.cpp: In function ‘int main()’: source_file.cpp:4:38: error: invalid static_cast from type ‘void()’ to type ‘void*’ std::cout<<static_cast<void*>(foo); ^ So, the question is

Do I Have to Specialize Templates If Their Offending Code Is in an if(false)

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-11 05:54:38
问题 Given the hierarchy: struct base {}; struct a : public base {}; struct b : public base {}; I want to fill vector<base*> vecBase and vector<a*> aVec with this function: template <typename T> void foo(T* bar) { if (is_base_of_v<decltype(baseVec)::value_type, T>) baseVec.push_back(static_cast<decltype(baseVec)::value_type>(bar)); if (is_base_of_v<decltype(aVec)::value_type, T>) baseVec.push_back(static_cast<decltype(aVec)::value_type>(bar)); } The problem here is that even though the static_cast

C2440 static_cast cannot convert from base class to derived class

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-11 05:35:26
问题 I am attempting to understand why casting from a base class to an derived class using pointers compiles fine, but casting using non-pointer object produces the error C2440. Below I have a base class ThreadedMessage that is inherited by class GPSMessage . struct ThreadedMessage { ThreadedMessage() : m_Type(0), m_ID(0) { } ThreadedMessage(uint Type, uint ID = 0) : m_Type(Type), m_ID(ID) { } uint m_Type; uint m_ID; }; struct GPSMessage : public ThreadedMessage { GPSMessage() : ThreadedMessage()

static_cast and temporary creation (final edition)

匆匆过客 提交于 2019-12-10 08:48:57
问题 Prerequisities: To understand this question, please, read the following question and its answer at first: Cast auto_ptr<Base> to auto_ptr<Derived> At Cast auto_ptr<Base> to auto_ptr<Derived> Steve answered that "Your static_cast would copy the auto_ptr to a temporary, and so aS would be reset and the resource would be destroyed when the temporary is (at the end of the statement)." I'm interested in the process of temporary creation while static_cast is called. I would like to have the code

Static cast vs. dymamic cast for traversing inheritance hierarchies

假装没事ソ 提交于 2019-12-09 18:24:07
问题 I saw one book on C++ mentioning that navigating inheritance hierarchies using static cast is more efficient than using dynamic cast. Example: #include <iostream> #include <typeinfo> using namespace std; class Shape { public: virtual ~Shape() {}; }; class Circle : public Shape {}; class Square : public Shape {}; class Other {}; int main() { Circle c; Shape* s = &c; // Upcast: normal and OK // More explicit but unnecessary: s = static_cast<Shape*>(&c); // (Since upcasting is such a safe and

Is my method for avoiding dynamic_cast<> faster than dynamic_cast<> itself?

不想你离开。 提交于 2019-12-09 10:53:37
问题 I was answering a question a few minutes ago and it raised to me another one: In one of my projects, I do some network message parsing. The messages are in the form of: [1 byte message type][2 bytes payload length][x bytes payload] The format and content of the payload are determined by the message type. I have a class hierarchy, based on a common class Message . To instantiate my messages, i have a static parsing method which gives back a Message* depending on the message type byte .

Cast from Void* to TYPE* using C++ style cast: static_cast or reinterpret_cast

雨燕双飞 提交于 2019-12-08 18:35:15
问题 So if your converting from Void* to Type* or from Type* to Void* should you use: void func(void *p) { Params *params = static_cast<Params*>(p); } or void func(void *p) { Params *params = reinterpret_cast<Params*>(p); } To me static_cast seems the more correct but I've seen both used for the same purpose. Also, does the direction of the conversion matter. i.e. should I still use static_cast for: _beginthread(func,0,static_cast<void*>(params) I have read the other questions on C++ style casting