天道酬勤,学无止境

c++

Does passing containers by value invalidate iterators?

Here is some example code: #include <iostream> #include <vector> template <typename T> std::vector<typename T::iterator> f(T t) { std::vector<typename T::iterator> v; for (auto i = t.begin(); i != t.end(); ++i) { v.push_back(i); } return v; } template <typename T> void print(const std::vector<T>& v) { for (auto i = v.begin(); i != v.end(); ++i) { std::cout << **i << ' '; } std::cout << std::endl; } int main() { std::vector<int> v{1, 2, 3}; print(f(v)); std::vector<std::vector<int>::iterator> itervec = f(v); print(itervec); } On ideone the output was: 1 2 3 163487776 2 3 Questions If I change f

2021-06-14 17:10:14    分类:问答    c++   iterator   containers

Execute integer templated function based on run-time parameter

I often have some prototype behaviour that generates output based on some design method. I template the design method, which enables a lot of functionality I need. However, sometimes the design method is given at runtime, so I'm usually required to write a huge switch statement. It usually looks like this: enum class Operation { A, B }; template<Operation O> void execute(); template<> void execute<A>() { // ... } template<> void execute<B>() { // ... } void execute(Operation o) { switch (o) { case Operation::A: return execute<Operation::A>(); case Operation::B: return execute<Operation::B>();

2021-06-14 17:08:07    分类:问答    c++   switch-statement   metaprogramming

The C++ implicit this, and exactly how it is pushed on the stack

I need to know whether, when a class method in C++ is called, the implicit 'this' pointer is the first argument, or the last. i.e: whether it is pushed onto the stack first or last. In other words, I'm asking whether a class method, being called, is taken by the compiler to be: int foo::bar(foo *const this, int arg1, int arg2); //or: int foo::bar(int arg1, int arg2, foo *const this); By extension therefore, and more importantly, that would also answer whether G++ would push the this pointer last or first, respectively. I interrogated google, but I didn't find much. And as a side note, when C++

2021-06-14 17:08:01    分类:问答    c++   assembly   stack   pointers   this

C++ compiler error c4430 “c++ doesnt support default int” [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 8 years ago. Hi im trying to define an alias called USHORT. // Demonstrates typedef keyword #include <iostream> typedef unsigned short int USHORT; // typedef defined main() { USHORT Width = 5; USHORT Length; Length = 10; USHORT Area = Width * Length; std::cout << "Width

2021-06-14 17:03:19    分类:问答    c++   int   alias   typedef

Calling non-static variable from static function

I am a beginner programmer in C++ and doing a PoC for my company. So I apologize for my basic question. class TestOne{ private: TestTwo* t2; void createInstance(TestTwo* param){ t2 = param; } static void staticFunctionToAccessT2(){ // Now here I want to access "t2" here in the current instance of the class // By current instance I mean "this" in non-static context // currently there is no function to get object, but can be created // ** we cannot call new TestOne(), because that will create a new instance // ** of the current class and that I don't want. } } Any help in this regard will be

2021-06-14 17:03:06    分类:问答    c++

MFC dlg class link errors for MyClass::GetMessageMap() and MyClass::GetRuntimeClass (MSVC 2008)

I copied an existing header for a dlg box class (created with the dlg class wizard/mfc wizard). All seemed to go fine until I added the cpp file to the project. Now i get odd link errors for some mfc magic methods: error LNK2001: unresolved external symbol "public: virtual struct CRuntimeClass * __thiscall DlgGapWindow::GetRuntimeClass(void)const " (?GetRuntimeClass@DlgGapWindow@@UBEPAUCRuntimeClass@@XZ) error LNK2001: unresolved external symbol "protected: virtual struct AFX_MSGMAP const * __thiscall DlgGapWindow::GetMessageMap(void)const " (?GetMessageMap@DlgGapWindow@@MBEPBUAFX_MSGMAP@@XZ)

2021-06-14 17:02:22    分类:问答    c++   visual-studio-2008   mfc   linker

“Custom intrinsic” function for x64 instead of inline assembly possible?

I am currently experimenting with the creation of highly-optimized, reusable functions for a library of mine. For instance, I write the function "is power of 2" the following way: template<class IntType> inline bool is_power_of_two( const IntType x ) { return (x != 0) && ((x & (x - 1)) == 0); } This is a portable, low-maintenance implementation as an inline C++ template. This code is compiled by VC++ 2008 to the following code with branches: is_power_of_two PROC test rcx, rcx je SHORT $LN3@is_power_o lea rax, QWORD PTR [rcx-1] test rax, rcx jne SHORT $LN3@is_power_o mov al, 1 ret 0 $LN3@is

2021-06-14 16:59:42    分类:问答    c++   assembly   64-bit   inline-assembly   intrinsics

When is the destructor of the temporary called

I wanted to know when the destructor of a temporay is called for both C++03 and C++11 Suppose I have the following case foo method() { foo f; ...... ...... return foo; } void doSomething() { foo f = method(); .... } Suppose I am using the flag -fno-elide-constructors since I would like to get a theoretical understanding of when the destructor of a temporary is called. So from the above code in C++03 when the method() is finished a copy of foo is made using its copy constructor. After that at the statement foo f = method() the copy constructor of foo is called again. In this case for C++03 when

2021-06-14 16:59:04    分类:问答    c++   c++11

Casting pointer from base type to child type

I'm building a simple game design for a project of mine. I have the following classes: class Character { public: virtual void Display(); virtual void SetParameters( char* param, ... ); }; class NonPlayableCharacter : public Character { public: virtual void Display(); virtual void SetParameters( char* paaram, ... ); int GetNPCState(); } And then I have a bunch of classes that derive either from Character or NonPlayableCharacter. I define it like so: std::vector<Character*> _allChar; My problem is that at any given time I would want to perform some operation on one of the element of the vector

2021-06-14 16:50:55    分类:问答    c++   polymorphism

Constructor arguments from a tuple

If I have a struct like: struct Thing { int x; int y; bool a; bool b; } Then I can create a Thing object by doing: Thing t {1,2,true,false};. However, if I have a tuple then I am doing something like: std::tuple<int, int, bool, bool> info = std::make_tuple(1,2,true,false); Thing t { std::get<0>(info), std::get<1>(info).. // and so on Is there a better way to do this?

2021-06-14 16:50:34    分类:问答    c++   struct   constructor   tuples