std

std::thread with pointer to data member

徘徊边缘 提交于 2019-12-10 15:07:22
问题 I was reading through the std::thread documentation at cppreference (not always 100% accurate, I know) and noticed the following definition for the behavior of std::thread when passed a "pointer-to-data-member" (not "pointer-to-member-function") as its first argument ( f ) and an object of the required class as its second argument ( t1 after copying to thread-local-storage): If N == 1 and f is pointer to a member data object of a class, then it is accessed. The value of the object is ignored.

What is the status of C++ TR2 Filesystem library?

梦想的初衷 提交于 2019-12-10 15:01:36
问题 What is the status of C++ TR2 Filesystem library as of last Bristol meeting? Will it be part of C++1Y (C++14?) or is it suspended or are there any known recent comments as of the last three meetings? 回答1: It has recently been unanimously approved by the ISO committee but did not make it in time for C++14. Though all the TSs (filesytem, library fundamentals, networking, etc.) are expected to be independently available ahead of C++17, I believe the intention is that if they are ready in time,

std::string memory leak

巧了我就是萌 提交于 2019-12-10 14:57:02
问题 I've got this class AppController and the function connectPlayer : /* AppController.h */ class AppController { // Some other declarations ... private: static const string TAG; }; /* AppController.cpp */ #include "AppController.h" const string AppController::TAG = "AppController"; AppController::AppController() { /* some code here...*/ } void AppController::connectPlayer() { std::string port; std::string host; port = CM->getMenu()->getData("PORT"); host = CM->getMenu()->getData("HOST"); this-

Map with multiple keys in C++

一世执手 提交于 2019-12-10 14:51:43
问题 I want to store data by both, their name and their index. In other words, I want to map string names to objects and also give them a custom order. What I came up with first is a std::vector of pairs of the string key and the object. The order was given by the position in the vector. std::vector<std::pair<std::string, object> > But this approach seems to be suboptimal since it doesn't automatically check for the uniqueness of string names. Moreover it feels wrong to group the objects by their

STL Vector of Unique_ptr - How to reset?

杀马特。学长 韩版系。学妹 提交于 2019-12-10 14:48:35
问题 I create two standard vector of unique_ptr: std::vector<std::unique_ptr<Student>> students; std::vector<std::unique_ptr<Teacher>> teachers; Then, I create a new object and put it in the vector: students.push_back(std::unique_ptr<Student> (new Student())); teachers.push_back(std::unique_ptr<Teacher> (new Teacher())); After all the operation I have to do, how can I delete the vector? Whitout unique_ptr I had to do a loop and delete each object: while (!students.empty()) { delete students.back()

Where can we use std::barrier over std::latch?

余生长醉 提交于 2019-12-10 14:42:40
问题 I recently heard new c++ standard features which are: std::latch std::barrier I cannot figure it out ,in which situations that they are applicable and useful over one-another. If someone can raise an example for how to use each one of them wisely it would be really helpful. 来源: https://stackoverflow.com/questions/48985967/where-can-we-use-stdbarrier-over-stdlatch

Value_type of a container template parameter

北城余情 提交于 2019-12-10 14:38:22
问题 In his keynote of this years Going Native The Essence of C++ (go to 40:30) Bjarne Stroustrup gives the following code example: template<typename C, typename V> vector<Value_type<C>*> find_all(C& cont, V v) { vector<Value_type<C>*> res; for (auto& x : cont) if (x == v) res.push_back(&x) return res; } This function is used to find all occurrences of a value in a container and returns pointers to the found elements. The example from the video: string m{"Mary had a little lamb"}; for (const auto

Why is set::find not a template?

元气小坏坏 提交于 2019-12-10 13:24:13
问题 With template functions from <algorithm> you can do things like this struct foo { int bar, baz; }; struct bar_less { // compare foo with foo bool operator()(const foo& lh, const foo& rh) const { return lh.bar < rh.bar; } template<typename T> // compare some T with foo bool operator()(T lh, const foo& rh) const { return lh < rh.bar; } template<typename T> // compare foo with some T bool operator()(const foo& lh, T rh) const { return lh.bar < rh; } }; int main() { foo foos[] = { {1, 2}, {2, 3},

How to return value of std::copy in case of success or failure?

北城以北 提交于 2019-12-10 13:17:12
问题 I am using std::copy to copy the objects in std::deque to a file. The code is working fine but I need to check if copying was successful and accordingly I need to set the flag or else throw exception. I have googled but could not find the solution how to check if std::copy has successfully copied the values into the file. Could someone please throw a light on it. 回答1: If writing to the file fails, then the file stream's error flags will be set - you can either check these after the copy, or

Valgrind is not showing invalid memory access with incorrectly used c_str()

梦想的初衷 提交于 2019-12-10 13:01:49
问题 Imagine such code: string f() { string r = "ab"; return r; } int main() { const char *c = f().c_str(); printf("%s.\n", c); return 0; } This code may crash, right? Because that string that c points to is destroyed. But running it via Valgrind doesn't show any invalid memory accesses. Why? I know Valgrind cannot check the stack, but "ab" actually is located on the heap, right? 回答1: This code may crash, right? Because that string that c points to is destroyed. Right. It has undefined behaviour,