Category : template-meta-programming

I have the following code intended to take a generic function object that takes two arguments and return a function object that does the same with the arguments in the other order. #include <functional> template <typename Function, typename FirstIn, typename SecondIn, typename std::enable_if<std::is_invocable<Function, FirstIn, SecondIn>::value>::type> std::function<typename std::invoke_result<Function, FirstIn, SecondIn>::type(SecondIn, FirstIn)> swapInput(Function f) { return[=](SecondIn b, ..

Read more

template<typename T> struct rm_const_volatile { using type = T; }; // Call this (partial)specialization as "1" template<typename T> struct rm_const_volatile<const T> { // remove topmost const and recurse on T using type = typename rm_const_volatile<T>::type; }; // Call this (partial)specialization as "2" template<typename T> struct rm_const_volatile<volatile T> { // remove topmost volatile and recurse on ..

Read more

I’m implementing compile time unit system, I am able to multiply different units together such that, for example: Scalar<int, M_>{2} * Scalar<int, M_>{2} == Scalar<int, M_, M_>{4}; I want also to be able to do this: Scalar<int, M_, M_>{4} / Scalar<int, M_>{2} == Scalar<int, M_>{2}; And I think a good place to start would be ..

Read more

Consider the following code: template<typename T, typename U> constexpr auto divide(T rhs, U lhs) { return rhs / static_cast<T>(lhs); } Here divide defaults to the rhs type (which is the same default behavior we get from the compiler. But what if we wanted to make divide smarter such that it would deduce in compile time ..

Read more

The mp-units library has a function they call the "downcasting facility" documented here This allows long template specializations to be given short aliases which can be automatically resolved by the compiler simply by inheriting a CRTP template. The very basic version of this is below: template<class BaseType> struct downcast_base { using downcast_base_type = BaseType; friend ..

Read more

the small piece of code below compiles and runs #include <iostream> using namespace std; template<class T> class A { public: template<class T2> void f(T2 a); }; template<class T> template<typename T2> void A<T>::f(T2 a){a();} int main() { A<int> ac; ac.f([](){cout<<"bla"<<endl;}); } but when split into files hpp: template<class T> class A { public: template<class T2> void ..

Read more

I stumbled upon this code in the SerenityOS project: template<typename… Parameters> void dbgln(CheckedFormatString<Parameters…>&& fmtstr, const Parameters&… parameters) They are re-implementing an equivalent of println! from rust. An easier version of printf where you don’t need to care about the argument type and is used this way: dbgln("This is a {}", "test"); They are doing some ..

Read more

In my program I have some code which looks something like this: A collection of functions or classes which uniquely implement a common template via template specialization: constexpr int NUM_SPECIALIZATIONS = 32; template<int num> void print_num(){} template<> void print_num<0>(){cout << "Zero" << endl;} template<> void print_num<1>(){cout << "One" << endl;} template<> void print_num<2>(){cout << "Two" ..

Read more