c++14

Algorithm for function evaluation by pairs (C++, STL)

ぐ巨炮叔叔 提交于 2019-12-07 04:29:35
问题 I need to apply a custom func to an STL containers by pairs -> that is: // if c => {a,b,c,d,e,f,g}; // a,b,c,.. are just aliases for some object my_algorithm(c.begin(),c.end(),[](auto a, auto b){ a + b }); // c++14 should resolve into something like this: temp1 = a + b; temp2 = c + d; temp3 = e + f; temp4 = temp1 + temp2; temp5 = temp3 + g; result = temp4 + temp5; (I am sure this kind of algorithm has a proper name but I have no idea what this may be) I have tried with std::accumulate , I am

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

不问归期 提交于 2019-12-07 04:11:40
问题 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

Practical limitations on amount of constexpr computation

假装没事ソ 提交于 2019-12-07 04:05:41
问题 As an experiment, I just put together some code to generate a std::array<uint32_t, 256> at compile time. The table contents themselves are a fairly typical CRC lookup table - about the only new thing is the use of constexpr functions to calculate the entries as opposed to putting an autogenerated magic table directly in the source code. Anyway, this exercise got me curious: would there be any practical limitations on the amount of computation a compiler would be willing to do to evaluate a

Variable templates + generic lambdas for std::map

独自空忆成欢 提交于 2019-12-07 04:04:04
问题 An answer to C++14 Variable Templates: what is the purpose? Any usage example? proposes a usage example of variable templates + generic lambdas that would look something like this: void some_func() { template<typename T> std::map<int, T> storage; auto store = []<typename T>(int key, const T& value) { storage<T>.insert(key, value) }; store(0, 2); store(1, "Hello"s); store(2, 0.7); // All three values are stored in a different map, according to their type. } Unfortunately it doesn't compile so

Cartesian Product using Iterators and Variadic Templates

馋奶兔 提交于 2019-12-07 03:47:50
问题 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

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

孤人 提交于 2019-12-07 03:35:29
问题 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

How can I return a lambda object?

*爱你&永不变心* 提交于 2019-12-07 02:53:34
问题 I want to return a lambda object from a function without casting it to a function pointer or function object. More specifically, I want to leave it to the client to decide whether or not to cast to a function object or retain the lambda as an anonymous function: template<typename F> // F may be a std::function, boost::function, or lambda object auto func(F f) -> decltype(???) // what do I put in here? { return [=]() { return f(someParameter); } } This code doesn't work because I don't know

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

爷,独闯天下 提交于 2019-12-07 02:50:35
问题 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? 回答1: 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

Does std::vector::assign/std::vector::operator=(const&) guarantee to reuse the buffer in `this`?

拈花ヽ惹草 提交于 2019-12-07 02:47:15
问题 If I assign or copy one vector to another (that has the same or bigger capacity than the size of the former), can I assume that the buffer of the latter will be reused? The following example demonstrates that I can, however, is it guaranteed by the standard? Is there any difference between behaviour of std::vector::assign and std::vector::operator= in this regard? #include <vector> #include <iostream> #include <cassert> int main() { std::vector a {1, 2, 3, 4, 5}; std::vector b {1, 2, 3, 4};

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

一笑奈何 提交于 2019-12-07 01:19:36
问题 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(); 回答1: The standard guarantees nothing. For shared_ptr