template-meta-programming

Detect if C++ lambda can be converted to function pointer

孤街浪徒 提交于 2019-12-10 02:34:54
问题 I have some code that generates assembly for a JIT idea I'm working on. I use meta-programming to generate calls by analyzing the function type and then generating the correct assembly to call it. I recently wanted to add lambda support, and lambdas have two versions, non-capturing (normal __cdecl function call) and capturing (__thiscall, member-function call with the lambda object as context). __thiscall is slightly more expensive so I'd like to avoid it whenever possible, and I'd also like

C++ - is it possible to extract class and argument types from a member function type in a template?

。_饼干妹妹 提交于 2019-12-09 10:53:14
问题 I would like to wrap member functions that conform to the type 'void (ClassType::Function)(ArgType)' with a templated class. Later, I want to pass an instance of ClassType to an instance of this template and have it invoke the wrapped method: class Foo { public: Foo() : f_(0.0) {} void set(double v) { f_ = v * 2.1; } double get() { return f_; } private: double f_; }; template <typename ArgType, typename ClassType, void (ClassType::*Method)(ArgType)> class Wrapper { public: explicit Wrapper

Force a compile time error in a template specialization

跟風遠走 提交于 2019-12-08 21:56:15
问题 I recently started to learn modern template metaprogramming and I wrote myself an index_of function for types. template<std::size_t Index, class C, class A> struct mp_index_of_impl{}; template<std::size_t Index, class C,template<class...> class A,class ...Ts> struct mp_index_of_impl<Index,C,A<C,Ts...>>{ using type = std::integral_constant<std::size_t,Index>; }; template<std::size_t Index, class C,template<class...> class A,class ...Ts,class T1> struct mp_index_of_impl<Index,C,A<T1,Ts...>>{

Nested loops unrolling using metaprogramming

左心房为你撑大大i 提交于 2019-12-08 16:05:25
问题 I have a number of nested loops with small sizes I, J, ... known at compile time, e.g. for(int i = 0; i < I; ++i) { for(int j = 0; j < J; ++j) { // ... // do sth with (i,j,...) } } I need to unroll the loops using the sizes I, J, ... in such a way that I can use each coordinate combination at compile time . To clarify, consider the following structure and take 2 nested loops with sizes I = 2, J = 3 . template<int... I> struct C { static void f() { // do sth } }; I can not use the indices i, j

Error when pass std::map as template template argument

流过昼夜 提交于 2019-12-08 08:33:27
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, above code is not able to compile. the error is like: error: template template argument has different

Implementing std::rank for other containers

*爱你&永不变心* 提交于 2019-12-08 07:44:32
问题 Explanation : std::rank just works for c style array . So I implemented similar rank for std::vector which works fine : #include <iostream> #include <vector> template<typename Type, Type val> struct integral_constant { static constexpr Type value =val; }; template<typename> struct rank : public integral_constant<std::size_t, 0> { }; template<typename Type> struct rank< std::vector<Type> > : public integral_constant<std::size_t, 1 + rank<Type>::value> { }; template<class T> constexpr size_t

Searching through a tuple for arguments of a function

被刻印的时光 ゝ 提交于 2019-12-08 07:39:47
问题 Consider this int foo (int a, char c, bool b) {std::cout << a << ' ' << c << ' ' << b << '\n'; return 8;} double bar (int a, char c, bool b, int d) {std::cout << a << ' ' << c << ' ' << b << ' ' << d << '\n'; return 2.5;} char baz (bool a, bool b) {std::cout << a << ' ' << b << '\n'; return 'a';} int main() { const auto tuple = std::make_tuple(5, true, 'a', 3.5, false, 1000, 't', 2, true, 5.8); const std::tuple<int, double, char> t = searchArguments (tuple, foo, bar, baz); } So the arguments

Matching template aliases as template template parameters

落花浮王杯 提交于 2019-12-08 07:28:51
问题 I'm currently writting a metafunction to evaluate expressions, something like boost::mpl::apply: template<typename EXPRESSION , typename... ARGS> using eval = typename eval_impl<EXPRESSION,ARGS...>::result; As you can see, I'm using C++11 template aliases to avoid writting typename ::result when using the evaluator. Among other specializations, eval_impl (The implementation of the evaluation metafunction) has an specializationfor the case the user passes a parametrized expression (Such as a

C++. How to define template parameter of type T for class A when class T needs a type A template parameter?

China☆狼群 提交于 2019-12-08 06:24:16
Executor class has template of type P and it takes a P object in constructor. Algo class has a template E and also has a static variable of type E. Processor class has template T and a collection of Ts. Question how can I define Executor< Processor<Algo> > and Algo<Executor> ? Is this possible? I see no way to defining this, its kind of an "infinite recursive template argument" See code. template <class T> class Processor { map<string,T> ts; void Process(string str, int i) { ts[str].Do(i); } } template <class P> class Executor { P &p; Executor(P &inp) : p(inp) {} void Bar(string str, int i) {

Template function for detecting pointer like (dereferencable) types fails for actual pointer types

拥有回忆 提交于 2019-12-08 05:38:23
问题 I am trying to write a mechanism to detect if a type is a pointer like type. By that I mean it is dereferencable through operator*() and operator->() . I have three different structs that are specialized accordingly: is_pointer_like_dereferencable which checks for operator*() is_pointer_like_arrow_dereferencable which checks for operator->() is_pointer_like which simply combines 1 & 2 I added specializations for non-templated types like int, int*, ... and for templated types like std::vector<