template-meta-programming

Overloading multiple function objects by reference

浪尽此生 提交于 2019-12-02 16:21:59
In C++17, it is trivial to implement an overload(fs...) function that, given any number of arguments fs... satisfying FunctionObject , returns a new function object that behaves like an overload of fs... . Example: template <typename... Ts> struct overloader : Ts... { template <typename... TArgs> overloader(TArgs&&... xs) : Ts{forward<TArgs>(xs)}... { } using Ts::operator()...; }; template <typename... Ts> auto overload(Ts&&... xs) { return overloader<decay_t<Ts>...>{forward<Ts>(xs)...}; } int main() { auto o = overload([](char){ cout << "CHAR"; }, [](int) { cout << "INT"; }); o('a'); //

How to properly use std::enable_if on a constructor

懵懂的女人 提交于 2019-12-02 14:35:58
问题 This question combines several pieces of code and is a bit complicated, but I tried slimming it down as much as possible. I am trying to use std::enable_if to conditionally invoke the correct constructor as a result of ambiguous function signatures when a lambda expression is used as input, but the parameters of said lambda expression can be implicitly convertible to one another. This is an attempt to build upon the following question: Here, but is sufficiently different and focuses on std:

What is the difference between a trait and a policy?

五迷三道 提交于 2019-12-02 13:51:53
I have a class whose behavior I am trying to configure. template<int ModeT, bool IsAsync, bool IsReentrant> ServerTraits; Then later on I have my server object itself: template<typename TraitsT> class Server {...}; My question is for my usage above is my naming misnamed? Is my templated parameter actually a policy instead of a trait? When is a templated argument a trait versus a policy? Policies Policies are classes (or class templates) to inject behavior into a parent class, typically through inheritance. Through decomposing a parent interface into orthogonal (independent) dimensions, policy

How to properly use std::enable_if on a constructor

狂风中的少年 提交于 2019-12-02 11:53:55
This question combines several pieces of code and is a bit complicated, but I tried slimming it down as much as possible. I am trying to use std::enable_if to conditionally invoke the correct constructor as a result of ambiguous function signatures when a lambda expression is used as input, but the parameters of said lambda expression can be implicitly convertible to one another. This is an attempt to build upon the following question: Here , but is sufficiently different and focuses on std::enable_if to merit another question. I am also providing the Live Example that works with the problem

partial type as template argument c++ [duplicate]

北战南征 提交于 2019-12-02 08:43:16
This question already has an answer here: What are some uses of template template parameters? 10 answers Simply, can I pass std::vector as a template argument. Following example list usage tempate<typename container_t, typename value_t> struct container_types { typedef container_t<value_t> value_container_t; typedef container_t<pair<value_t> pair_container_t; }; I wish to pass container_types to generate final two data types. If this is not feasible, then how can I achieve the same. IdeaHat Yes, like this #include <iostream> #include <vector> template <template<typename T> class container_t,

Accessing base member data error when derived class is templated

可紊 提交于 2019-12-02 06:52:47
I have the following problem with the curiously recurring template, with a problem when I try to access the data member of CRTP base class. template<typename T> struct Base { int protectedData=10; }; struct Derived : public Base<Derived> { public: void method() { std::cout<<protectedData<<std::endl; }; }; int main () { Derived a; a.method(); } The above code compiles and runs fine and I can get "10" printed, but if I have the derived class templated, like: template<typename T> struct Base { int protectedData=10; }; template<typename T> struct Derived : public Base<Derived<T> > { public: void

Find untagged template options/parameters/args by position

孤街浪徒 提交于 2019-12-02 06:41:59
In short: I want to extract various options from variadic template parameters, but not only by tag but by index for those parameters, that have no known tag. I like the approach in boost (e.g. heap or lockfree policies), but want to make it compatible with STL containers - the allocator parameter. Preface I am currently writing template for queue/buffer of variable-size records/objects with this signature: // current: template <typename R = byte, class Alloc = std::allocator<byte>, class... Opts> class rqueue; // what I want: template <class... Opts> class rqueue; I have few other possible

Creating an API / metaprogramming DSL for static initialization of a layout description

99封情书 提交于 2019-12-02 03:43:55
问题 I need to create a C++ metaprogramming DSL/API to describe generic data layouts. I want to have the data structure descriptions initialized statically (i.e. no dynamic memory allocation, preferably allocation in the ROM section). I'm not yet very familiar with C++11 features but I'm aware you can do a lot more about initializing (statically) than the old standard provided. I'm looking for a solution that makes it easy for a client to describe the data layout using some kind of 'basic'

Creating an API / metaprogramming DSL for static initialization of a layout description

我的梦境 提交于 2019-12-02 00:18:23
I need to create a C++ metaprogramming DSL/API to describe generic data layouts. I want to have the data structure descriptions initialized statically (i.e. no dynamic memory allocation, preferably allocation in the ROM section). I'm not yet very familiar with C++11 features but I'm aware you can do a lot more about initializing (statically) than the old standard provided. I'm looking for a solution that makes it easy for a client to describe the data layout using some kind of 'basic' DataItemDescriptor like this: template < typename ItemIdType , typename OffsetType = unsigned int > struct

Boost hana get index of first matching

耗尽温柔 提交于 2019-12-01 23:41:57
So I am trying to make a library using boost::hana that requires the functionality to get the index of a element based on the value: constexpr auto tup = boost::hana::make_tuple(3_c, boost::hana::type_c<bool>); auto index = get_index_of_first_matching(tup, boost::hana::type_c<bool>); // ^^^^^ would be a boost::hana::int_<1> Is there a possible way to do this? Better yet, is it already in hana and I don't know about it? Thanks for the support! Hana does not provide an algorithm to do this out-of-the-box. If it seems like a much desired feature, I could add such an algorithm fairly easily. It