c++-standard-library

How to correctly use std::reference_wrappers

无人久伴 提交于 2019-11-28 17:48:56
I am trying to understand std::reference_wrapper . The following code shows that the reference wrapper does not behave exactly like a reference. #include <iostream> #include <vector> #include <functional> int main() { std::vector<int> numbers = {1, 3, 0, -8, 5, 3, 1}; auto referenceWrapper = std::ref(numbers); std::vector<int>& reference = numbers; std::cout << reference[3] << std::endl; std::cout << referenceWrapper.get()[3] << std::endl; // I need to use get ^ // otherwise does not compile. return 0; } If I understand it correctly, the implicit conversion does not apply to calling member

std::atomic | compare_exchange_weak vs. compare_exchange_strong

青春壹個敷衍的年華 提交于 2019-11-28 16:52:18
I'm unsure if it's me not understanding or the documentation isn't clearly formulated. The following excerpt has been taken from the newest draft (N3126, section 29.6): bool atomic_compare_exchange_weak(volatile A* object, C * expected, C desired); bool atomic_compare_exchange_weak(A* object, C * expected, C desired); bool atomic_compare_exchange_strong(volatile A* object, C * expected, C desired); bool atomic_compare_exchange_strong(A* object, C * expected, C desired); bool atomic_compare_exchange_weak_explicit(volatile A* object, C * expected, C desired, memory_order success, memory_order

std::string.resize() and std::string.length()

最后都变了- 提交于 2019-11-28 14:18:35
I'm relatively new to C++ and I'm still getting to grips with the C++ Standard Library. To help transition from C, I want to format a std::string using printf-style formatters. I realise stringstream is a more type-safe approach, but I find myself finding printf-style much easier to read and deal with (at least, for the time being). This is my function: using namespace std; string formatStdString(const string &format, ...) { va_list va; string output; size_t needed; size_t used; va_start(va, format); needed = vsnprintf(&output[0], 0, format.c_str(), va); output.resize(needed + 1); // for null

std::istream operator exception reset / not thrown

巧了我就是萌 提交于 2019-11-28 13:58:03
I'm not sure about how to use std::istream::exception according to the standard , to let std::istream::operator>> throw an exception if it can't read the input into a variable, e.g. double. The following code has different behavior with clang/libc++ and gcc/libstdc++: #include <iostream> #include <cassert> int main () { double foo,bar; std::istream& is = std::cin; is.exceptions(std::istream::failbit); is >> foo; //throws exception as expected with gcc/libstdc++ with input "ASD" std::cout << foo; is >> bar; std::cout << bar; assert(is); //failed with clang/libc++ after input "ASD" std::cout <<

Should std::list::size have constant complexity in C++11?

谁说胖子不能爱 提交于 2019-11-28 13:20:55
I am using gcc 4.8.1 and after hours of debugging a horrible mysterious performance issue I found out that the std::list::size is actually implemented as a call to std::distance . /** Returns the number of elements in the %list. */ size_type size() const _GLIBCXX_NOEXCEPT { return std::distance(begin(), end()); } This surprised me, since the reference says that the complexity of std::list::size should be constant and the complexity of std::distance is linear for std::list::iterator . I am really confused, since I think gcc has excellent support for C++11 features and I see no reason why they

No <optional> in MS Visual Studio 2013 - what to do?

我的梦境 提交于 2019-11-28 11:10:45
I want to use std::experimental::optional , but MSVS 2013 tells me it can't find the header. Why isn't it there? Can I roll my own based on code elsewhere? The C++14 proposal maybe? std::experimental::optional originates from the Boost.Optional library, and this implementation works well in Visual C++ 12.0 (though it differs a little ). Reference single-header implementation, based on the N3793 proposal paper, can be found here . The latest list of supported C++11/14/1z core and library features that are shipped with Visual Studio can be found from the Visual C++ Team blog , from this post in

Is there a use case for std::function that is not covered by function pointers, or is it just syntactic sugar? [duplicate]

自作多情 提交于 2019-11-28 09:13:57
This question already has an answer here: Why do we use std::function in C++ rather than the original C function pointer? [duplicate] 3 answers The notation for std::function is quite nice when compared to function pointers. However, other than that, I can't find a use case where it couldn't be replaced by pointers. So is it just syntactic sugar for function pointers? std::function<> gives you the possibility of encapsulating any type of callable object , which is something function pointers cannot do (although it is true that non-capturing lambdas can be converted to function pointers). To

Can I take the address of a function defined in standard library?

拜拜、爱过 提交于 2019-11-28 08:58:04
Consider the following code: #include <cctype> #include <functional> #include <iostream> int main() { std::invoke(std::boolalpha, std::cout); // #1 using ctype_func = int(*)(int); char c = std::invoke(static_cast<ctype_func>(std::tolower), 'A'); // #2 std::cout << c << "\n"; } Here, the two calls to std::invoke are labeled for future reference. The expected output is: a Is the expected output guaranteed in C++20? (Note: there are two functions called tolower — one in <cctype> and the other in <locale> . The explicit cast is introduced to select the desired overload.) Short answer No.

`std::pair` `second` has incomplete type with `unordered_map` tree

守給你的承諾、 提交于 2019-11-28 08:39:16
问题 I was reviewing some older code of mine and I saw the code using pointers to implement a tree of Variant objects. It is a tree because each Variant can contain an unordered_map of Variant* . I looked at the code and wondered why isn't it just using values, a std::vector<Variant> , and std::unordered_map<std::string, Variant> , instead of Variant* . So I went ahead and changed it. It seemed okay except one thing, I got errors: /usr/local/include/c++/6.1.0/bits/stl_pair.h:153:11: error: 'std:

Returning std::move(f) in std::for_each

有些话、适合烂在心里 提交于 2019-11-28 07:37:07
问题 I'm writing an implementation of standard c++ library for study. The C++11 standard says that for_each returns std::move(f) . template <class InputIterator, class Function> Function for_each(InputIterator first, InputIterator last, Function f); Returns: std::move(f). I thought that function scope local variable is move-constructed when it's returned. Should I return move(f) explicitly? 回答1: From Josuttis 's The C++ Standard Library You don’t have to and should not move() return values.