template-meta-programming

Map two types at compile time

蹲街弑〆低调 提交于 2019-12-23 09:33:56
问题 I have a set of types related with a one-to-one relation, for example: TypeA ---> Type1 TypeB ---> Type2 TypeC ---> Type3 I know these relation at compile time. Then, I have a template class that depends on this two types: template<class T1,class T2> class MyClass { T1 foo; T2 bar; }; Now, the user of my library will type something like: MyClass<TypeA,Type1> x; This is inconvenient because there is a dependency between the two types and it should be enough for the user specify only the first

How to Deduce Argument List from Function Pointer?

烂漫一生 提交于 2019-12-23 08:49:46
问题 Given two or more example functions, is it possible to write templated code which would be able to deduce the arguments of a function provided as a template parameter? This is the motivating example: void do_something(int value, double amount) { std::cout << (value * amount) << std::endl; } void do_something_else(std::string const& first, double & second, int third) { for(char c : first) if(third / c == 0) second += 13.7; } template<void(*Func)(/*???*/)> struct wrapper { using Args = /*???*/;

Unify type and non-type template parameters

倖福魔咒の 提交于 2019-12-23 07:21:42
问题 I have a type trait that checks if a given type is an instance of a given class template: template <template <typename...> class C, typename T> struct check_is_instance_of : std::false_type { }; template <template <typename...> class C, typename ...Ts> struct check_is_instance_of<C, C<Ts...>> : std::true_type { }; template <template <typename...> class C, typename T> struct is_instance_of : check_is_instance_of<C, std::remove_cv_t<T>> { }; Unfortunately, this does not work for non-type

Function argument type

≡放荡痞女 提交于 2019-12-22 11:16:58
问题 My code is supposed is to determine if the given function takes the given type as a parameter. Answering your future "what for" questions I will shortly answer: to use it with boost::enable_if template. The code uses decltype operator of the C++11. My question is: Is it possible to achieve the same goal using c++03? #include <iostream> template <class F, class P> struct has_arg_of_type { static bool const value = false; }; template <class R, class A> struct has_arg_of_type<R (A), A> { static

Constexpr counter that works on GCC 8, and is not restricted to namespace scope

馋奶兔 提交于 2019-12-22 10:35:52
问题 I'm trying to learn some arcane stateful template metaprogramming tricks. (Here's why I want to learn it. Unfortunately this library doesn't work on GCC 8 nor on Clang.) The first obvious thing I need is a constexpr counter: /*something*/ constexpr int foo() /*something*/ int main() { constexpr int a = foo(); constexpr int b = foo(); constexpr int c = foo(); static_assert(a == 0 && b == 1 && c == 2); } Preferably it should be a tagged counter, so that I can have several counters at the same

How to count the number of CRTP subclasses of a template class?

我只是一个虾纸丫 提交于 2019-12-22 06:15:10
问题 Does anyone know of a method to use CRTP to count the number of subclasses of an object? Suppose we had a setup similar to the following one: template <typename T> class Object { .... }; const unsigned int ObjectSubClassCount = ...; class Subobject : public Object<SubObject> { .... }; class Second : public Object<Second> { .... }; and so on, such that, using TMP, we might have a constant ( ObjectSubClassCount ) that represents the total number of subclasses? Does anyone know a way to do this?

How to extract the highest-indexed specialization from a structure?

给你一囗甜甜゛ 提交于 2019-12-22 03:46:53
问题 I'm trying to do some template metaprogramming and I'm finding the need to "extract" the highest index of a specialization of some structure in some type. For example, if I have some types: struct A { template<unsigned int> struct D; template<> struct D<0> { }; }; struct B { template<unsigned int> struct D; template<> struct D<0> { }; template<> struct D<1> { }; }; struct C { template<unsigned int> struct D; template<> struct D<0> { }; template<> struct D<1> { }; template<> struct D<2> { }; }

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

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-22 00:23:24
问题 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. 回答1: With C++11: #include <iostream> template <typename F> struct Traits; template

Perform overload resolution with template meta-programming

允我心安 提交于 2019-12-21 20:23:12
问题 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(

How to unroll a for loop using template metaprogramming

纵饮孤独 提交于 2019-12-21 20:13:43
问题 How do you write a simple C++ code to simply run a for loop with a certain unrolling factor? For instance, I need to write a for loop that assigns a value of i to each index of the array, i.e, A[i]=i for array size lets say 1e6. Now I want to add an unrolling factor of lets say 20. I do not want to manually write 20 lines of code and iterate it 5k times. How do I do this? Do I nest my for loop? Does the compiler automatically do some unrolling for me if I use template metaprogramming? And how