template-meta-programming

Error when pass std::map as template template argument

耗尽温柔 提交于 2019-12-08 05:14:12
问题 I defined a function like this, in which there is a template template class template<typename Key, typename Value, template <typename, typename> class Map> struct ForEachOf { void operator()(const Map<Key, Value>& map, std::function<void (Key, Value)> func) { for(const auto& pair : map) { func(pair.first, pair.second); } } }; std::map<int, string> m { {1, "foo"}, {3, "bar"}}; ForEachOf<int, string, std::map> forEachOf; forEachOf(m, [](int key, string value) { cout << key << value; }); However

Compile time hash with constexpr

落花浮王杯 提交于 2019-12-08 04:14:03
问题 I have found this example/class in a book for creating SDBM hashes at compile time. Unfortunately it does not compile (neither with c++11 nor c++14). I am getting error: call to non-constexpr function . I've tried around a little bit, but I can't seem to make this work. So here is my question: Why is it not working and how could it be fixed? (I am sorry, I know it's a generic question, but at least for a very specific case) Full (not working) example for you to test: #include <iostream>

Compile time hash with constexpr

試著忘記壹切 提交于 2019-12-08 04:03:24
I have found this example/class in a book for creating SDBM hashes at compile time. Unfortunately it does not compile (neither with c++11 nor c++14). I am getting error: call to non-constexpr function . I've tried around a little bit, but I can't seem to make this work. So here is my question: Why is it not working and how could it be fixed? (I am sorry, I know it's a generic question, but at least for a very specific case) Full (not working) example for you to test: #include <iostream> template <int stringLength> struct SDBMCalculator { static inline int Calculate(const char* const

Initializing `constexpr` Array with Pattern

此生再无相见时 提交于 2019-12-08 02:27:22
问题 I would like to initialize a constexpr array with a pattern that is generated using variadic template parameters. For simplicity, consider the problem of initializing a constexpr unsigned static array with the sizes of a list of types, say, unsigned, short, char, int, long . How can I do this so that all of the computation is done during compile time? I need the solution to play nice with the C++ type system, so I cannot use macros. The best I could come up with is shown below, but

Get the subset of a parameter pack based on a given set of indices

落花浮王杯 提交于 2019-12-08 01:34:04
问题 Okay, this is a really difficult one. I want to be able to get the subset of a parameter pack by choosing the parameter types at a given set of valid indices and then use that parameter pack as the argument list of a function. IE: template <size_t... indices_t> void func(pack_subset<indices_t..., args_t...>); //incorrect syntax, //not sure how else to write this //args_t is predetermined, this //function is a static member of //a variadic class template //For a parameter pack <float, double>

type wrapper error with boost::mpl::for_each (section 9.1.1 from Abrahams & Gurtovoy book)

瘦欲@ 提交于 2019-12-07 23:00:02
问题 The following code is copied almost verbatim from section 9.1.1 of the book C++ Template Metaprogramming: Concepts, Tools, and Techniques from Boost and Beyond by David Abrahams & Aleksey Gurtovoy. The only change is that I want to be able to change the type wrapper template from the book with the regular Boost template mpl::identity. However, under Microsoft Visual C++ Express 2010 (SP1) I get a mysterious compiler warning if I do that. It seems somehow related to the fact that the type

How can I arbitrarily sort a tuple's types?

拥有回忆 提交于 2019-12-07 18:52:09
问题 One thing that really annoys me about C++ is that an empty struct / class takes up space. So, I have this idea that std::tuple (or some variant, since it's (and the compiler's) implementation is highly implementation dependent) might be able to save the day, which it sort of does, but there are issues due to packing and alignment. Because of how compilers will align the items in the struct , having a empty next to a non-empty next to an empty next to a non-empty will be larger than 2 empties

Get deepest class in CRTP inheritance chain

江枫思渺然 提交于 2019-12-07 18:01:53
问题 I would like to know how to solve the following problem (C++17): suppose there are several template classes, inherited from each other in CRTP-like fashion (single inheritance only). For a given instantiated template base class, find the class that is furthest from it down the inheritance chain. I first thought that is should be pretty easy, but was not able to accomplish this. To simplify, suppose that every root and every intermediate class has using DerivedT = Derived in its public area.

Boost MPL to generate code for object serialization?

北慕城南 提交于 2019-12-07 12:48:08
问题 I want to generate serialization/deserialization code for class Object { string a; int b; long c; char d; }; by looking at a mpl sequence, but I need to be able to identify object and retrieve it back as well, I can't figure out how would I get the names of it members, do I have to know it ? code should look like void SerializeObject(ostream os) { serialize(object.a, os); serialize(object.b, os); //serialize(object.member, os); } I want to generate above code by user only defining a mpl

Build template from arguments of functions?

对着背影说爱祢 提交于 2019-12-07 12:29:24
问题 template<class... Foos> // N = sizeof...(Foos) template<typename... Args> // M = sizeof...(Args) void split_and_call(Args&&... args) { // Using Python notation here... Foos[0](*args[:a]); // a = arity of Foos[0] Foos[1](*args[a:b]); // b-a = arity of Foos[1] ... Foos[N-1](*args[z:M]); // M-z = arity of Foos[N-1] } Assumptions: All types in Foos are callable All types in Foos are unambiguous Any type in Foos may have an arity of 0 Args is the concatenation of all of the argument types used by