std

std::error_code, my_error::check_block == my_error::validate && my_error::accept_block == my_error::validate

爱⌒轻易说出口 提交于 2020-01-01 09:13:06
问题 I'm using std::error_code and have a bunch of error's defined (using enum class) and registered. I have a very generic error now called my_error::validate, but want to provide more specific versions in my library. Generally people will want to use: if (ec == bc::error::validate) // ... However sometimes they may wish to see the specific error associated with that std::error_code or print the error message. // ec.message() says "check_block() failed to do XYZ" assert(ec == bc::error::check

How does std::vector's copy constructor operate?

筅森魡賤 提交于 2020-01-01 08:37:09
问题 How does a std::vector<std::string> initialize its self when the following code is invoked std::vector<std::string> original; std::vector<std::string> newVector = original; It would seem as if the copy constructor would be invoked on std::vector<std::string> new during newVector = original , but how are the std::string 's brought over inside of the orginal ? Are they copies or new std::string 's? So is the memory in newVector[0] the same as original[0] . The reason I ask is say I do the

Where does nullptr_t reside?

时光怂恿深爱的人放手 提交于 2020-01-01 05:30:16
问题 A bit of prehistory. I've been writing a game engine for quite some time. It's divided into several static libraries, like "utils", "rsbin" (resource system), "window", which are then linked into a single executable. It is a crossplatform engine, being compiled for Windows and for Android. Under Windows, I compile it with MinGW. Under Android, with CCTools, which is an interface to native gcc. One of the base classes is utils::RefObject , which represents a concept similar to Windows's

Using a templated parameter's value_type

巧了我就是萌 提交于 2020-01-01 04:23:06
问题 How is one supposed to use a std container's value_type? I tried to use it like so: #include <vector> using namespace std; template <typename T> class TSContainer { private: T container; public: void push(T::value_type& item) { container.push_back(item); } T::value_type pop() { T::value_type item = container.pop_front(); return item; } }; int main() { int i = 1; TSContainer<vector<int> > tsc; tsc.push(i); int v = tsc.pop(); } But this results in: prog.cpp:10: error: ‘T::value_type’ is not a

ifstream, end of line and move to next line?

穿精又带淫゛_ 提交于 2020-01-01 02:41:22
问题 how do i detect and move to the next line using std::ifstream? void readData(ifstream& in) { string sz; getline(in, sz); cout << sz <<endl; int v; for(int i=0; in.good(); i++) { in >> v; if (in.good()) cout << v << " "; } in.seekg(0, ios::beg); sz.clear(); getline(in, sz); cout << sz <<endl; //no longer reads } I know good would tell me if an error happened but the stream no longer works once that happens. How can i check to see if i am at the end of line before reading another int? 回答1: Use

Initializer list inside std::pair

£可爱£侵袭症+ 提交于 2020-01-01 01:13:11
问题 This code: #include <iostream> #include <string> std::pair<std::initializer_list<std::string>, int> groups{ { "A", "B" }, 0 }; int main() { for (const auto& i : groups.first) { std::cout << i << '\n'; } return 0; } compiles but returns segfault. Why? Tested on gcc 8.3.0 and on online compilers. 回答1: std::initializer_list is not meant to be stored, it is just meant for ... well initialization. Internally it just stores a pointer to the first element and the size. In your code the std::string

using out of scope variables in C++11 lambda expressions

廉价感情. 提交于 2019-12-31 20:30:30
问题 I'm playing with C++11 for fun. I'm wondering why this happens: //... std::vector<P_EndPoint> agents; P_CommunicationProtocol requestPacket; //... bool repeated = std::any_of(agents.begin(), agents.end(), [](P_EndPoint i)->bool {return requestPacket.identity().id()==i.id();}); Compilation terminates with this error: error: 'requestPacket' has not been declared Which is declared earlier in code. I tried ::requestPacke and it doesn't worked too. How can I use an external scope variable inside a

Where do std::bind-created functors live?

人走茶凉 提交于 2019-12-30 18:09:04
问题 A function pointer can point to anything from a free function, a function object, a wrapper over a member function call. However, the std::bind created functors can have state, as well as custom-created ones. Where that state is allocated, and who is deleting it? Consider the below example - will the state ( the number 10) be deleted when the vector is deleted? Who know to call a deleter on the functor, and no deleter on the function pointer? #include <iostream> #include <functional> #include

Where do std::bind-created functors live?

你说的曾经没有我的故事 提交于 2019-12-30 18:08:19
问题 A function pointer can point to anything from a free function, a function object, a wrapper over a member function call. However, the std::bind created functors can have state, as well as custom-created ones. Where that state is allocated, and who is deleting it? Consider the below example - will the state ( the number 10) be deleted when the vector is deleted? Who know to call a deleter on the functor, and no deleter on the function pointer? #include <iostream> #include <functional> #include

Why is std::mutex taking a long, highly irregular amount of time to be shared?

被刻印的时光 ゝ 提交于 2019-12-30 11:03:50
问题 This code demonstrates that the mutex is being shared between two threads, but one thread has it nearly all of the time. #include <thread> #include <mutex> #include <iostream> #include <unistd.h> int main () { std::mutex m; std::thread t ([&] () { while (true) { { std::lock_guard <std::mutex> thread_lock (m); sleep (1); // or whatever } std::cerr << "#"; std::cerr.flush (); } }); while (true) { std::lock_guard <std::mutex> main_lock (m); std::cerr << "."; std::cerr.flush (); } } Compiled with