c++14

A way to filter range by indices, to get min_element from filtered indices only?

笑着哭i 提交于 2019-12-05 07:54:32
In comments to this question is-there-a-way-to-iterate-over-at-most-n-elements-using-range-based-for-loop there was additional question - is this possible to have "index view" on a container, i.e. to have subrange with some indexes filtered out. Additionally I encountered a problem to find minimum value from a range with some indexes filtered out. I.e. is it possible to replace such code as below with std and/or boost algorithms, filters - to make it more readable and maintainable: template <typename Range, typename IndexPredicate> auto findMin(const Range& range, IndexPredicate ipred) ->

Cartesian Product using Iterators and Variadic Templates

≡放荡痞女 提交于 2019-12-05 07:46:14
I'm trying to create a function to generate the Cartesian product of a variable number of input ranges, using the style of the STL. My basic format is that the function accepts a fixed range and the start of an output range, then a variadic number of bidirectional input iterators. template < typename BidirectionalIterator, typename OutputIterator, typename... Args > void cartesian_product( BidirectionalIterator first, BidirectionalIterator last, OutputIterator result, Args&&... args ); My idea for the args is that I make a tuple out of it, then I iterate through that tuple to extract the

How does Apple clang-703.0.29 map back to clang releases in terms of C++1x support?

痴心易碎 提交于 2019-12-05 06:32:33
I want to map Apple's clang which is shipped with Xcode back to the official clang feature list. But I couldn't link the two. The version is obscured. Is there a way to tell? I didn't quite understand why would you need such information, since they already claim full compatibility with C++11 spec . Anyway, here's my take on it. Please note that following steps will not reveal a clear answer but currently I can't think of a better approach to this. This developer curated list suggests that Apple LLVM version 7.3.0 clang 703.0.29 is bundled with Xcode 7.3 (7D175) . When I go to the project page

Compilation of C++14 in qtcreator

空扰寡人 提交于 2019-12-05 06:16:50
I have a qt project containing parts in C++14 . Recently, I changed my ubuntu distro. Now I have 16.04 LTS and I installed Qt creator 4.02 (built on jun 13). In order to enable C++14 compilation, I put in the project file: QMAKE_CXXFLAGS += -std=c++14 However, when building project, the IDE generates the following command: g++ -c -pipe -std=c++14 -g -O0 -g -std=gnu++11 -Wall -W -D_REENTRANT ... As seen, the generated makefile puts the flag -std=gnu++11 which overrides the flag for C++14 . This did not happen with my previous distro (LTS 12.04, same qt creator version). I tried with CONFIG +=

c++11/14 make_unique ambigious overload for std::string

自闭症网瘾萝莉.ら 提交于 2019-12-05 06:14:49
Could someone please explain how to resolve the ambigious overload warning for make_unique, where the error comes from and what it does exactly mean (I do understand what an ambigious overload is but I am unsure why I get one for this particular code)? I am using c++11, therefore I use the recommended template from Herb Sutter. Using it I get the following error: Error 4 error C2668: 'make_unique' : ambiguous call to overloaded function And the hover over tooltip in visual studio 13 gives me the following to methods: function template "std::enable_if<!std::is_array<_Ty>::value, std::unique_ptr

C++14 Metaprogramming: Automagically build a list of types at compile / init time

主宰稳场 提交于 2019-12-05 06:11:51
Using C++14 and some combination of the Curiously Recurring Template Pattern (CRTP) and possibly Boost.Hana (or boost::mpl if you wish), can I build a list of types at compile time (or static initialization time) without an explicit declaration? As an example, I have something like this (see it on Coliru ): #include <iostream> #include <boost/hana/tuple.hpp> #include <boost/hana/for_each.hpp> namespace { struct D1 { static constexpr auto val = 10; }; struct D2 { static constexpr auto val = 20; }; struct D3 { static constexpr auto val = 30; }; } int main() { // How to avoid explicitly defining

Perfect forwarding in a lambda?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-05 05:53:31
With a function, one can write: template <class T> void f(T&& x) {myfunction(std::forward<T>(x));} but with a lambda, we don't have T : auto f = [](auto&& x){myfunction(std::forward</*?*/>(x));} How to do perfect-forwarding in a lambda? Does decltype(x) work as the type in std::forward ? Kerrek SB The canonical way to forward a lambda argument that was bound to a forwarding reference is indeed with decltype : auto f = [](auto&& x){ myfunction(std::forward<decltype(x)>(x)); } // ^^^^^^^^^^^ My favorite idiom for this is: auto f = [](auto&& x){myfunction(decltype(x)(x));} which I read as " x as

Casting a char array to an object pointer - is this UB?

二次信任 提交于 2019-12-05 05:45:25
I recently saw a class like this that was used to construct objects "on-demand" without having to use dynamic memory allocation for various reasons. #include <cassert> template<typename T> class StaticObject { public: StaticObject() : constructed_(false) { } ~StaticObject() { if (constructed_) ((T*)object_)->~T(); } void construct() { assert(!constructed_); new ((T*)object_) T; constructed_ = true; } T& operator*() { assert(constructed_); return *((T*)object_); } const T& operator*() const { assert(constructed_); return *((T*)object_); } private: bool constructed_; alignas(alignof(T)) char

Are weak pointers guaranteed to have expired by the time the std::shared_ptr deleter runs?

一个人想着一个人 提交于 2019-12-05 05:43:22
If I have a std::shared_ptr<Foo> with a custom deleter, is it guaranteed that all associated weak pointers are seen as expired by the deleter? (I would appreciate it very much if you could cite relevant sections in the standard.) In other words is the assertion below guaranteed not to fire? std::weak_ptr<Foo> weak; std::shared_ptr<Foo> strong{ new Foo, [&weak] (Foo* f) { assert(weak.expired()); delete f; }, }; weak = strong; strong.reset(); The standard guarantees nothing. For shared_ptr 's destructor, the spec only says: If *this is empty or shares ownership with another shared_ptr instance (

Custom allocators vs. promises and packaged tasks

流过昼夜 提交于 2019-12-05 05:39:00
Are the allocator-taking constructors of standard promise / packaged_task supposed to use the allocator for just the state object itself, or should this be guaranteed for all (internal) related objects? [futures.promise]: "...allocate memory for the shared state" [futures.task.members]: "...allocate memory needed to store the internal data structures" In particular, are the below bugs or features? *MSVC 2013.4, Boost 1.57, short_alloc.h by Howard Hinnant Example 1 #define BOOST_THREAD_VERSION 4 #include <boost/thread/future.hpp> #include "short_alloc.h" #include <cstdio> void *operator new(