template-meta-programming

How to call a function object differently, depending on its arity (or other information known at compile time)?

瘦欲@ 提交于 2019-12-04 19:26:08
In a function template, I'd like to call a function, or function object differently, depending on its arity (how many arguments it takes). In pseudocode: if arity(f) == 1: f(x) if arity(f) == 2: f(x, y) if arity(f) == 3: f(x, y, z) How can this be done in C++? Edit To clarify the difficulty: f(x, y, z) won't compile if f only takes 2 arguments, and vice versa, f(x, y) won't compile when f needs 3 arguments. With C++11: #include <iostream> template <typename F> struct Traits; template <typename R, typename... A> struct Traits<R (A...)> { static constexpr unsigned Arity = sizeof...(A); }; void f

Compiletime for each with custom functions

*爱你&永不变心* 提交于 2019-12-04 18:39:00
Abstract: Imagine a problem of the following form: One has to invoke multiple specific member functions with the same parameters on a list of functors. That makes a good problem to solve with an interface (runtime_interface, in other words a requirement of functions that those functors have to implement). The Problem I would like to discuss is the case where the list of functors is known at compile time, but might be subject to change during the further development process. Because in this case if implemented like that one is paying the runtime overhead even though all the functions to be

Metaprogramming with std::is_same

巧了我就是萌 提交于 2019-12-04 18:25:25
问题 Is it possible to do something like the following that compiles without template specialization? template <class T> class A { public: #if std::is_same<T,int> void has_int() {} #elif std::is_same<T,char> void has_char() {} #endif }; A<int> a; a.has_int(); A<char> b; b.has_char(); 回答1: Yes. Make the function templates and then conditionaly enable them using std::enable_if: #include <type_traits> template <class T> class A { public: template<typename U = T> typename std::enable_if<std::is_same<U

Template metaprogramming recursion up limits?

别说谁变了你拦得住时间么 提交于 2019-12-04 16:39:26
问题 I am writing a very simple template class using Metaprogramming to compute sum in compile time, as below: #include <iostream> using namespace std; template<int N> class Sum { public: enum {value = N + Sum<N-1>::value }; }; template<> class Sum<0> { public: enum {value = 0}; }; int main() { cout << Sum<501>::value << endl; } The interesting thing is: When I print Sum<500> and below, it works fine When it comes to Sum<501>, the compile failed with: sum.cpp:9: instantiated from Sum<500>' sum.cpp

Perfect forwarding of functions to build a function list class

我的梦境 提交于 2019-12-04 16:30:50
Consider the following code that build a class storing functions. // Function list class template <class... F> struct function_list { template <class... G> constexpr function_list(G&&... g) noexcept : _f{std::forward<G>(g)...} { } std::tuple</* F... OR F&&... */> _f; }; // Function list maker template <class... F, class R = /* Can we compute the return type here? */> constexpr R make_function_list(F&&... f) { return function_list< /* decltype(std::forward<F>(f))... * OR F... * OR F&&... */>(std::forward<F>(f)...); } I would like these functions to be perfectly forwarded (regardless of whether

Perform overload resolution with template meta-programming

余生颓废 提交于 2019-12-04 14:41:50
Inspired by another question I tried to find a way to deduce the type of an overload member function given the actual argument used to call that function. Here is what I have so far: #include <type_traits> template<typename F, typename Arg> struct mem_fun_type { // perform overload resolution here typedef decltype(std::declval<F>()(std::declval<Arg>())) result_type; typedef decltype(static_cast<result_type (F::*)(Arg)>(&F::operator())) type; }; struct foo {}; struct takes_two { void operator()(int); void operator()(foo); }; struct take_one { void operator()(float); }; int main() { static

How to define a variant<x,y,z> extracting subtypes of a template parameter

笑着哭i 提交于 2019-12-04 13:21:16
I am building a state-machine where state transitions are described as a variant, i.e.: using table = std::variant< /* state event followup-state */ transition<start, success<sock>, connecting>, transition<start, exception, failed>, transition<connecting, success<>, connected>, transition<connecting, exception, failed>, transition<connected, exception, failed> >; and transition being a simple type: template <typename ENTRY_STATE, typename EVENT, typename NEXT_STATE> struct transition { using entry_state = ENTRY_STATE; using event = EVENT; using next_state = NEXT_STATE; }; The state classes are

Detect same class inheritance with SFINAE

大兔子大兔子 提交于 2019-12-04 10:55:36
I'm trying to write a metafunction that checks whether all types passed as a variadic template parameter are distinct. It seems that the most performant way to do this is to inherit from a set of classes and detect, whether there is an error. The problem is that compilation fails in the following code, while I would expect SFINAE to work. Edit. The question is not "how to write that metafunction" but "how do I catch that double inheritance error and output false_type when it happens". AFAIK, it's possible only with SFINAE. template <typename T> struct dummy {}; // error: duplicate base type

boost.proto + modify expression tree in place

牧云@^-^@ 提交于 2019-12-04 10:04:41
Background question: boost.proto + detect invalid terminal before building the expression tree . Hi, what i'm trying to achieve is create a copy of an expression tree, where all vectors are substituted with their begin iterators (in my case is a raw pointer) increment the iterators in place dereference iterators in the tree, but that part should be relatively easy. So, for 1. I ended up with this code /////////////////////////////////////////////////////////////////////////////// // A transform that converts all vectors nodes in a tree to iterator nodes struct vector_begin : proto::transform

Can I overload functions with type-traits?

南笙酒味 提交于 2019-12-04 09:12:30
问题 Let's say, I have six types, and they each belong in a conceptual category. Here is a diagram that shows this: Or Perhaps a more specific example for you: I want to write two functions that will handle all 6 types. Types in "Category 1" get handled a certain way, and types in "Category 2" get handled a different way. Let's get into the code. First, I'll create the six types. //Category 1 Types class Type_A{}; class Type_B{}; class Type_C{}; //Category 2 Types class Type_D{}; class Type_E{};