std

Lifetime of returned strings and their .c_str() [duplicate]

天涯浪子 提交于 2019-12-11 01:37:59
问题 This question already has answers here : C++: Life span of temporary arguments? (4 answers) Closed 2 years ago . I've come across multiple instance of this pattern (with boost::filesystem only used as example): boost::filesystem::path path = ...; someFunctionTakingCStrings(path.string().c_str()); where const std::string path::string() const { std::string tmp = ... return tmp; } Although I have never experienced problem with this pattern, I was wondering when the string returned by sting() is

Why doesn’t std::string have a virtual destructor?

可紊 提交于 2019-12-11 01:21:14
问题 When I was working on a project that involved defining sentences in a given language, I was surprised to discover that std::string destructor was not virtual. This made it a lot more difficult to specialize this class (I had to create a wrapper). Why did the standard committee decide to have this class not virtual? in /usr/lib64/gcc/x86_64-pc-linux-gnu/4.5.3/include/g++-v4/bits/basic_string.h, we have: template<typename _CharT, typename _Traits, typename _Alloc> class basic_string { ... /** *

debug websocket++ broadcast_server.cpp ('owner_less' is not a member of 'std')

丶灬走出姿态 提交于 2019-12-11 00:23:12
问题 Zaphoyd's broadcast_server.cpp looks like the perfect backbone for a websocket server that it can quickly accept and send messages & connections with a thread for the real action so not to interrupt the communications. https://github.com/zaphoyd/websocketpp/blob/experimental/examples/broadcast_server/broadcast_server.cpp His simple print_server.cpp example compiles easily; however, I'm getting a few compile errors with broadcast_server.cpp . root@server:~# g++ -O3 broadcast_server.cpp -I ~

How can I make sure there are a given number of threads at all times? (Also, is this a good usage of threads?)

荒凉一梦 提交于 2019-12-11 00:08:48
问题 I've just started diving into multithreading using the standard library today. Below is what I've come up with so far. Though it works in principle, it does not start a new thread once one has finished, but rather starts 4 threads once the last 4 finished, so if the tasks take an unequal amount of time, it isn't very efficient. Also, if they don't finish in the correct order, they have to wait for the succeeding work to finish until their results are evaluated. How could I achieve always

Why does std::logic_error not virtually inherit from std::exception?

时光怂恿深爱的人放手 提交于 2019-12-10 23:44:40
问题 I'm trying to implement a custom exception hierarchy and allow appropriate std::* to be caught by code. class my_exception : public virtual std::exception { }; class my_bad_widget_state : public virtual my_exception, public virtual std::logic_error { public: my_bad_widget_state() : std::logic_error("widget oops") {} }; Obviously my_bad_widget_state is a my_exception and is also a std::logic_error, but the compiler rejects this code because std::exception doesn't say virtual when inheriting

Extending namespace std with backported types and templates from newer C++ standards

风流意气都作罢 提交于 2019-12-10 23:10:14
问题 Currently we work on a library that is allowed to use C++11/14 features and types when possible but must fallback to a C++03 implementation otherwise. Users and developers of this library are encouraged to use types and templates from namespace std . Problems arise when using types from namespace std introduced by newer versions of the C++ Standard which are not shipped with the compiler used. One suggested option is to provide 'backports' for certain types that do not rely on new language

Why the MVS compiler can convert argument 'myStruct' to 'myStruct &'. And did not file error C2664: cannot convert 'myStruct' to 'myStruct &'

与世无争的帅哥 提交于 2019-12-10 23:09:31
问题 I have a class where I use std::mem_fn to choose between helper functions. Why my code get compiled and run If I am missing & in m_funcContainer deceleration ? In the code & commented out with /**/ myStruct/*&*/ std::map < std::string, std::function<void(const myClass*, myStruct/*&*/) >> m_funcContainer (but in case of m_funcContainerInt the compiler rise compile error) error C2664: 'void (int &) const' : cannot convert argument 1 from 'int' to 'int &' I feel that I have not formulate the

How to define a copy constructor for a const template parameter?

我的未来我决定 提交于 2019-12-10 21:52:27
问题 I'm creating a custom iterator and I'm having trouble satisfying the scenario where I create a const iterator and initialize it with a non- const begin() . This is legal according to the STL and can be demonstrated with std::string: #include <string> using namespace std; int main() { string::iterator a; string::const_iterator b = a; return 0; } I can't figure out how to make it work: template<typename T> class some_class { }; int main() { some_class<int> a; // Works OK! const some_class<int>

Setting std::vector contents directly using operator []

倖福魔咒の 提交于 2019-12-10 21:47:39
问题 I notice that if I do vec[0] = 1 with a fresh empty std::vector<int> , vec.size() remains 0 . However I can then still do vec[0] to retrieve my 1 . Is this in the realm of undefined behavior? What is going on here? Is this simply writing the 1 into reserved memory space, and I should expect this to blow up in my face, right? 回答1: It is indeed Undefined behavior [] Does not check bounds and does not add elements . What you are doing is writing to and reading from the buffer which vector uses,

Inconsistent behavior of std::regex

末鹿安然 提交于 2019-12-10 18:23:40
问题 I have the following issue where: std::regex behaves differently if I pass the result of boost::filesystem::path::string() vs storing the result in a intermediate string variable. The first will return a match that is truncated and which is later not accepted by std::stoull (throws invalid_argument exception) while the second works perfectly. See the following commands that explain more the issue: [nix-shell:~]$ ls -l foo total 0 -rw-r--r-- 1 amine users 0 Aug 10 16:55 008 -rw-r--r-- 1 amine