c++-standard-library

Is Stephen Lavavej's Mallocator the same in C++11?

让人想犯罪 __ 提交于 2019-12-09 05:27:49
问题 8 years ago, Stephen Lavavej published this blog post containing a simple allocator implementation, named the "Mallocator". Since then we've transitioned to the era of C++11 (and soon C++17) ... does the new language features and rules affect the Mallocator at all, or is it still relevant as is? 回答1: STL himself has an answer to this question in his STL Features and Implementation techniques talk at CppCon 2014 (Starting at 26'30). The slides are on github. I merged the content of slides 28

Getting “Debug Assertion Failed!” for set comparator

怎甘沉沦 提交于 2019-12-08 17:43:44
问题 I know similar issue has been answered at this link Help me fix this C++ std::set comparator but unfortunately I am facing exactly same issue and I am unable to understand the reason behind it thus need some help to resolve it. I am using VS2010 and my release binary is running fine without any issue but the debug binary reports: My comparator looks like this: struct PathComp { bool operator() (const wchar_t* path1, const wchar_t* path2) const { int c = wcscmp(path1, path2); if (c < 0 || c >

Initialising std::discrete_distribution in VS2013

这一生的挚爱 提交于 2019-12-08 16:29:25
问题 I have a std::vector<float> weights; containing the list of weights. I don't know what will be in this list until some stage in running the program. I would like to do std::discrete_distribution<> dist(weights.begin(),weights.end()); but VS2013 doesn't appear to have a constructor for std::discrete_distribution that accepts an iterator range. Is there any workaround? 回答1: Compare cppreference.com and the Microsoft reference for std::discrete_distribution : These are the constructors provided

Why do standard containers require allocator_type::value_type to be the element type?

旧巷老猫 提交于 2019-12-07 12:14:00
问题 Related: Deprecation of std::allocator<void>. The following description about template parameter Allocator is found for both std::vector and std::list (emphasis mine): An allocator that is used to acquire/release memory and to construct/destroy the elements in that memory. The type must meet the requirements of Allocator. The behavior is undefined if Allocator::value_type is not the same as T . The last sentence does not make sense to me. If a specific value_type is required, couldn't it just

Comparison for objects derived from std::string_view is ambiguous in MSVC

点点圈 提交于 2019-12-07 04:02:26
问题 TL;DR: Can I expect that the code below will compile on any c++17 conformant c++ toolchain (based on the current c++17 proposal) and the failure of MSVC to do so is a bug in their implementation? #include <string_view> struct Foo : std::string_view {}; int main() { Foo f1{}; Foo f2{}; return f1 == f2; } Explanation: I have a class that is derived from std::string_view and doesn't implement its own comparison operators, because the std::string_view semantics are exactly what I need and I also

Must a C++ Standard Library be implemented in C++?

自作多情 提交于 2019-12-06 19:00:35
问题 Must a conforming C++ Standard Library Implementation be implemented in C++? If not, is it allowed to do magic things that are not doable in pure C++ & Standard Library & some implementation defined behaviour? I am aware that there are parallel implementations that rely on extensions (in pre C++11 at least), but are they really conforming? I couldn't find any requirement in the standard, but maybe my standard-fu is weak today 回答1: No. Actually, it is even prescribed by the Standard that

What is wrong with my usage of C++ standard library's find?

不羁的心 提交于 2019-12-06 17:47:59
问题 I'm trying to use the C++ standard library's find algorithm like this: template<class T> const unsigned int AdjacencyList<T>::_index_for_node( const std::vector<T>& list, const T& node ) throw(NoSuchNodeException) { std::vector<T>::iterator iter = std::find(list.begin(), list.end(), node); } When I try to compile, I get these errors: In file included from ../AdjacencyList.cpp:8: ../AdjacencyList.h: In member function ‘const unsigned int Graph::AdjacencyList<T>::_index_for_node(const std:

nextafter vs nexttoward functions in C++ 2011?

江枫思渺然 提交于 2019-12-06 17:14:37
问题 What is the difference between the nextafter and the nexttoward functions of the C++ 2011 standard library ? 回答1: Since the functions originate from C, they can't be overloaded, which means two different names for functions that do the same but have different parameter(-type)s. Here are the original signatures: float nextafter(float, float); float nexttoward(float, long double); And now the standard just says there should be a few overloads to make things nicer in C++ ( §26.8 [c.math] p11 ):

Exception safety issue of std::make_unique()

一曲冷凌霜 提交于 2019-12-06 11:53:08
问题 From this thread, we know that the following code is exception-safe (assuming that T( U(std::move(v)) ) is exception-safe, e.g., does not throw, which is library user's responsibility). auto p = new T( U(std::move(v)) ); The key point here is that the initializer expression U(std::move(v)) is evaluated after memory allocation. Now consider the std::make_unique() counterpart. auto p = std::make_unique<T>( U(std::move(v)) ); The initializer expression U(std::move(v)) is evaluated even before

Why calling shared_from_this calls std::terminate

空扰寡人 提交于 2019-12-06 09:03:35
Consider this code: class A : public std::enable_shared_from_this<A> { public: std::shared_ptr<A> f() { return shared_from_this(); } }; int main() { A a; std::shared_ptr<A> ptr = a.f(); } This code terminated in Visual Studio 2017. I guess I am doing something wrong here. Can anyone help me with this? I want a shared_ptr on a created by shared_from_this(). Because a is not a owned by a shared pointer. From cppreference : It is permitted to call shared_from_this only on a previously shared object, i.e. on an object managed by std::shared_ptr. Otherwise the behavior is undefined (until C++17)std