static-cast

Reading in from a .txt file to a struct array that contains enum

大憨熊 提交于 2020-01-17 12:12:09
问题 Here is my code enum Status {IN, OUT }; const int TITLE_SIZE = 50, ISBN_SIZE = 13, AUTHOR_SIZE = 25; struct Info { char title[TITLE_SIZE]; char isbn[ISBN_SIZE]; char author[AUTHOR_SIZE]; Status inOrOut; }; int main() { fstream dataFile; string filename; int numOfBooks = 0; Info *test = 0; int enumHolder = 0; cout << "How many books does the file contain? "; cin >> numOfBooks; test = new Info[numOfBooks]; cout << "Enter a file (with path) for input and output: "; cin >> filename; dataFile.open

static_cast parent class to child class C++

こ雲淡風輕ζ 提交于 2020-01-17 05:13:13
问题 Output of this program is "Method B". How can an instance of the parent object call the child class's function through a static_cast? To make things more confusing, if I make method() virtual, then this code outputs "Method A". Can anyone explain what is happening here? class A { public: void method() { cout << "Method A" << endl; } }; class B : public A { public: void method() { cout << "Method B" << endl; } }; int main() { A a; B* bptr = static_cast<B*>(&a); bptr->method(); } 来源: https:/

Why is the downcast in CRTP defined behaviour

安稳与你 提交于 2020-01-06 04:46:09
问题 I have used the CRTP pattern for a while, however reading answers about undefined behaviour related to downcasting I don't understand why the static_cast<Derived&>(this) , where this is of type Base<Derived>* is defined behaviour, where Derived inherits publicly from Base<Derived> . The standard is quite clear: static_cast < new_type > ( expression ) If new_type is a pointer or reference to some class D and the type of expression is a pointer or reference to its non-virtual base B, static

How to static cast throwing function pointer to noexcept in C++17?

杀马特。学长 韩版系。学妹 提交于 2020-01-03 06:59:07
问题 C++17 makes noexcept part of a function's type. It also allows implicit conversions from noexcept function pointers to potentially throwing function pointers. void (*ptr_to_noexcept)() noexcept = nullptr; void (*ptr_to_throwing)() = ptr_to_noexcept; // implicit conversion http://eel.is/c++draft/expr.static.cast#7 says that static_cast can perform the inverse of such a conversion. void (*noexcept_again)() noexcept = static_cast<void(*)() noexcept>(ptr_to_throwing); Unfortunately, both GCC and

How to implement a compile-time check that a downcast is valid in a CRTP?

做~自己de王妃 提交于 2020-01-02 02:33:10
问题 I have a plain old CRPT (please don't get distracted by access restrictions - the question is not about them): template<class Derived> class Base { void MethodToOverride() { // generic stuff here } void ProblematicMethod() { static_cast<Derived*>(this)->MethodToOverride(); } }; that is as usual intended to be used like this: class ConcreteDerived : public Base<ConcreteDerived> { void MethodToOverride() { //custom stuff here, then maybe Base::MethodToOverride(); } }; Now that static_cast

What wording in the C++ standard allows static_cast<non-void-type*>(malloc(N)); to work?

こ雲淡風輕ζ 提交于 2020-01-01 05:11:11
问题 As far as I understand the wording in 5.2.9 Static cast, the only time the result of a void* -to-object-pointer conversion is allowed is when the void* was a result of the inverse conversion in the first place. Throughout the standard there is a bunch of references to the representation of a pointer, and the representation of a void pointer being the same as that of a char pointer, and so on, but it never seems to explicitly say that casting an arbitrary void pointer yields a pointer to the

What does reinterpret_cast<char *>(&st) and (-1)*static_cast<int> mean?

扶醉桌前 提交于 2019-12-26 03:13:42
问题 The code here is being used for creating a Student Report card project. While trying to understand we can not figure out the use of and functions of the below code: File.read(reinterpret_cast<char *> (&st), sizeof(student)); int pos=(-1)*static_cast<int>(sizeof(st)); File.read(reinterpret_cast<char *> (&st), sizeof(student)); if(st.retrollno()==n) { st.showdata(); cout<<"\n\nPlease Enter The New Details of student"<<endl; st.getdata(); int pos=(-1)*static_cast<int>(sizeof(st)); File.seekp(pos

CGAL static_cast failing

允我心安 提交于 2019-12-25 08:33:48
问题 For my thesis I am using some CGAL code written by another student a year ago. I cannot get it to build, however. The function that is giving errors is the following: std::set<Curve_handle> originatingCurves(PL_Arrangement arrangement, VertexHandleSet vertices) { std::set<Curve_handle> curves; for (Vertex_handle vertex : vertices) { auto heStart = vertex->incident_halfedges(); auto heCurrent = vertex->incident_halfedges(); do { Halfedge_handle handle = static_cast<Halfedge_handle>(heCurrent);

What's the advantages of turning off RTTI from compiler setting?

有些话、适合烂在心里 提交于 2019-12-23 19:15:32
问题 By this(How expensive is RTTI?), it seems clear that dynamic casting is much expensive than static type comparison, but I wonder if it would be worth to turn off RTTI option in compiler option(VS2010, /GR-) I have no dynamic cast in my code(I replaced them with static cast). But does (/GR-) option do any other than emitting errors when using dynamic cast? Is there any memory or code optimization in there? Thanks in advance. 回答1: Straight from MSDN (emphasis mine): When /GR is on, the compiler

Can static_cast to same type introduce runtime overhead?

心已入冬 提交于 2019-12-23 07:25:59
问题 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