c++11

Best way to create a setter function in C++

橙三吉。 提交于 2021-02-18 12:32:05
问题 I want to write a template function that receives parameter by move or by copy. The most efficient way that I use is: void setA(A a) { m_a = std::move(a); } Here, when we use is A a; setA(a); // <<---- one copy ctor & one move ctor setA(std::move(a)); // <<---- two move ctors I recently found out that defining it this way, with two functions: void setA(A&& a) { m_a = std::move(a); } void setA(const A& a) { m_a = a; // of course we can so "m_a = std::move(a);" too, since it will do nothing }

Is it possible to match recursively integer template parameters in C++?

∥☆過路亽.° 提交于 2021-02-18 11:11:51
问题 I have the following problem. I define a N dimensional vector as so #include <vector> #include <utility> #include <string> template <int N, typename T> struct NVector{ typedef std::vector<typename NVector<N-1,T>::type> type; }; template <typename T> struct NVector<1,T> { typedef std::vector<T> type; }; I wish to write a higher order function Map that can transform the leaf elements of the nested vector no matter how deep and return a new nested vector of the same shape. I have tried template

Is it possible to match recursively integer template parameters in C++?

僤鯓⒐⒋嵵緔 提交于 2021-02-18 11:11:26
问题 I have the following problem. I define a N dimensional vector as so #include <vector> #include <utility> #include <string> template <int N, typename T> struct NVector{ typedef std::vector<typename NVector<N-1,T>::type> type; }; template <typename T> struct NVector<1,T> { typedef std::vector<T> type; }; I wish to write a higher order function Map that can transform the leaf elements of the nested vector no matter how deep and return a new nested vector of the same shape. I have tried template

Optimization techniques used by std::regex_constants::optimize

六月ゝ 毕业季﹏ 提交于 2021-02-18 10:58:43
问题 I am working with std::regex , and whilst reading about the various constants defined in std::regex_constants , I came across std::optimize , reading about it, it sounds like it is useful in my application (I only need one instance of the regex, initialized at the beginning, but it is used multiple times throughout the loading process). According to the working paper n3126 (pg. 1077), std::regex_constants::optimize : Specifies that the regular expression engine should pay more attention to

Pushing an object with unique_ptr into vector in C++

一曲冷凌霜 提交于 2021-02-18 10:40:15
问题 I have a simple class structure modelling a discrete simulation, with a vector of States, which each contain a number of Transitions, held as a vector of smart pointers. I've used smart pointers to hold the transitions as in my full application I need polymorphism. #include <vector> #include <memory> class Transition { public: Transition() {} }; class State { public: State(int num) : num(num), transitions() {} void add_transition(std::unique_ptr<Transition> trans) { transitions.push_back(std:

Pushing an object with unique_ptr into vector in C++

可紊 提交于 2021-02-18 10:40:09
问题 I have a simple class structure modelling a discrete simulation, with a vector of States, which each contain a number of Transitions, held as a vector of smart pointers. I've used smart pointers to hold the transitions as in my full application I need polymorphism. #include <vector> #include <memory> class Transition { public: Transition() {} }; class State { public: State(int num) : num(num), transitions() {} void add_transition(std::unique_ptr<Transition> trans) { transitions.push_back(std:

MinGW g++ doesn't find headers in its own include directory

十年热恋 提交于 2021-02-18 10:34:22
问题 So I recently installed MinGW via the latest version of nuwen's MinGW distribution that includes the boost C++ libraries. Specifically, I was after the scoped_ptr that the boost library provides. However, when I try to include the scoped_ptr ( #include <boost/scoped_ptr.hpp> ) in my header, the compiler throws error: boost/scoped_ptr.hpp: No such file or directory Makefile: compile: g++ -o gen/cavestory src/**.cc run: gen/cavestory Also, I added a back version of SDL to MinGW's include

Is it safe to pass arguments by reference into a std::thread function?

蹲街弑〆低调 提交于 2021-02-18 09:58:09
问题 #include <thread> #include <string> #include <vector> #include <chrono> using namespace std; void f(const vector<string>& coll) { this_thread::sleep_for(1h); // // Is coll guaranteed to be valid before exiting this function? // } int main() { { vector<string> coll(1024 * 1024 * 100); thread(f, coll).detach(); } // // I know std::thread will copy arguments into itself by default, // but I don't know whether these copied objects are still valid // after the std::thread object has been destroyed

Can range-based for loops be aware of the end?

天大地大妈咪最大 提交于 2021-02-18 09:52:13
问题 Given the minimal C++11 STL example: set<int> S = {1,2,3,4}; for(auto &x: S) { cout << x; cout << ","; } Is there a way to check if x is the one right before the end? The goal in this example is to output 1,2,3,4 and not the final comma at the end. Currently I use a standard for loop with two iterators, set<int>::const_iterator itr; set<int>::const_iterator penultimate_end_itr = --S.end(); for(itr=S.begin(); itr!=penultimate_end_itr;++itr) cout << (*itr) << ','; cout << (*penultimate_end_itr)

Can range-based for loops be aware of the end?

限于喜欢 提交于 2021-02-18 09:51:30
问题 Given the minimal C++11 STL example: set<int> S = {1,2,3,4}; for(auto &x: S) { cout << x; cout << ","; } Is there a way to check if x is the one right before the end? The goal in this example is to output 1,2,3,4 and not the final comma at the end. Currently I use a standard for loop with two iterators, set<int>::const_iterator itr; set<int>::const_iterator penultimate_end_itr = --S.end(); for(itr=S.begin(); itr!=penultimate_end_itr;++itr) cout << (*itr) << ','; cout << (*penultimate_end_itr)