std

std::thread <unresolved overloaded function type> error

僤鯓⒐⒋嵵緔 提交于 2019-12-09 14:06:09
问题 I am trying to spawn a thread from within my class and the thread executes a particular method in my class. The code looks like this: class ThreadClass{ int myThread(int arg){ // do something } void createThread(){ thread t = thread(myThread,10); } } ; This code on compilation throws an error saying std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = int (ThreadClass::*)(int), _Args = {int}] no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘int

Why is stringstreams rdbuf() and str() giving me different output?

大憨熊 提交于 2019-12-09 13:24:48
问题 I have this code, int main() { std::string st; std::stringstream ss; ss<<"hej hej med dig"<<std::endl; std::getline(ss,st,' '); std::cout <<"ss.rdbuf()->str() : " << ss.rdbuf()->str(); std::cout <<"ss.rdbuf() : " << ss.rdbuf(); return 0; } Giving me this output ss.rdbuf()->str() : hej hej med dig ss.rdbuf() : hej med dig But why is that? Is that because of ostreams definition of operator<str() gives me different output. In my eyes the output should be the same even if I have used getline. 回答1

For which purposes needs std::aligned_storage?

早过忘川 提交于 2019-12-09 09:20:39
问题 So, as I understended correctly, the main advantage of the std::aligned_storage is that it manage the align. Also it could be copied with memcpy. Also it is usable only with POD types. But! 1) The POD types receive some align from compiler by default and we can remove the align by #pragma pack(push, 1) 2) We can copy POD with memcpy by default (we shouldnt do something for this ability) So I cant get actually for which purposes do we need the std::aligned_storage? 回答1: You can use std:

Convert uint64_t to std::string

你说的曾经没有我的故事 提交于 2019-12-09 08:59:01
问题 How can I transfer uint64_t value to std::string? I need to construct the std::string containing this value For example something like this: void genString(uint64_t val) { std::string str; //.....some code for str str+=(unsigned int)val;//???? } Thank you 回答1: use either boost::lexical_cast or std::ostringstream e.g.: str += boost::lexical_cast<std::string>(val); or std::ostringstream o; o << val; str += o.str(); 回答2: In C++ 11 you may just use: std::to_string() it's defined in header http:/

How to check if a std::async task is finished?

China☆狼群 提交于 2019-12-09 05:15:03
问题 In my graphics application I want to generate a batch meshes in another thread. Therefore I asynchrony call the member function using std::async . task = async(launch::async, &Class::Meshing, this, Data(...)); In my update loop I try to check if the thread is ready. If yes, I will send the mesh to the video card and start the next thread. If not, I will skip these operations. #include <future> using namespace std; class Class { public: void Update() { if(task.finished()) // this method does

boost::optional alternative in C++ Standard Library

给你一囗甜甜゛ 提交于 2019-12-08 23:37:49
问题 I'm trying to get my program working without boost usage, but can't find an alternative of some useful patterns. Namely, I can't find boost::optional -likewise pattern in the standard library. Is there some standard alternative for boost::optional (C++11 or somewhere else)? 回答1: Short answer: No. Long answer: Roll your own according to the boost spec. The documentation is quite exhaustive and the code isn't that complex, but this still requires above average C++ skills. To update this answer:

C++ create string of text and variables

本小妞迷上赌 提交于 2019-12-08 23:03:25
问题 I'm trying to do something very simple and yet, after an hour of so of searching a I can't find a suitable answer so I must be missing something fairly obvious. I'm trying to dynamically create filenames for use with ifstream. Whilst I understand various methods are available of doing this, I have settled on creating a std::string, and the using stringname.c_str to convert to const. The problem is however that I need to create the string with a mix of variables and predefined text values. I'm

std::thread c++. More threads same data

偶尔善良 提交于 2019-12-08 21:53:58
问题 Im using visual studio 2012 and c++11. I dont understand why this does not work: void client_loop(bool &run) { while ( run ); } int main() { bool running = true; std::thread t(&client_loop,std::ref(running)); running = false ; t.join(); } In this case, the loop of thread t never finishes but I explicity set running to false . run and running have the same location. I tried to set running as a single global variable but nothing happens. I tried to pass a pointer value too but nothing. The

Difference between std::search and std::find_first_of

柔情痞子 提交于 2019-12-08 21:47:33
问题 I am trying to grasp the difference between std::search and std::find_first_of They have the same prototypes: template <class ForwardIterator1, class ForwardIterator2> ForwardIterator1 find_first_of (ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2); template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate> ForwardIterator1 find_first_of (ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2,

Why is std::bitset::size non-static

余生颓废 提交于 2019-12-08 18:48:40
问题 I can't possibly imagine why it was chose that std::bitset::size is non-static. It makes it much harder to get a constexpr size; you have to write something like this: template<int val> struct int_ { static const constexpr value = val; }; template<size_t size> auto getBitsetSizeIMPL(std::bitset<size>) { return int_<size>{}; } template<typename BitsetType> constexpr size_t getBitsetSize() { return decltype(getBitsetSizeIMPL(BitsetType{}))::value; } When if it were static all you would have to