Category : variadic-templates

I have a somewhat contrived piece of code: #include <functional> template <typename… ARGs> auto construct1(std::function<void(ARGs…)> p, const ARGs &…args) {} template < typename… ARGs> auto construct2(std::function<void(int)> p, const ARGs &…args) {} int main() { auto p = [](int) {}; construct1<int>(p, 0); construct2<int>(p, 0); return 0; } Why is the compiler struggling to work out that ..

Read more

I have a variadic template functor class: template <typename Result, typename… Arguments> class Functor { public: using FunctionType = std::function<Result(Arguments…)>; Result operator() (const Arguments&… arguments) { return Function(arguments); } std::string GetName() { return Name; } Functor(const std::string& name, const FunctionType& function) : Name(name), Function(function) { } private: std::string Name; FunctionType Function; } and a variadic ..

Read more

The following code snippet can only be linked if optimization level was higher than O0: #include <cstdio> #include <utility> void vf(std::size_t n, …) {printf("%zun", n);} template<typename …ARGS> void vt(ARGS&&… args) {vf( sizeof…(ARGS), std::forward<ARGS>(args)… );} struct X {static constexpr int a = 123; X() {vt(a);}}; int main() {X();} You can run it here: http://cpp.sh/3dv7p Configuring C++11/14 ..

Read more

How can I replace 1 type from variadic template by its index? For example I have: //————-static_list————- template<class …Elements> struct static_list {}; struct execute_command<index, static_list<Commands…>> { static constexpr int value = execute_s<index + 1, static_list<Commands…>>::value; } and I want to rewrite it so in Commands it will replace type at for example index 5 with ..

Read more

Consider the following code template<typename Derived, typename …T > struct A; template<whatever> struct buz { … auto xyz() { … } template <typename T> auto xyz(int) { …. } }; template<typename Derived, typename …T > struct A<Derived, std::tuple<T…> > : crtp<Derived, A<Derived, std::tuple<T…>>> { auto foo() { return std::make_tuple (Helper<T>(this->derived().something )…); } void bar() { ..

Read more

I have templates for instructions like add, store, jump, return and variable. Then I created static_list template for storing these instructions. I want to execute them one by one. I use enable_if_else and comparing member instruction for checking what type the current instruction is and branch accordingly. However, I cant continue after the first check, ..

Read more

I have a 3rd party library, which has a function requiring a callback function with the following signature: typedef void (*callback1) (void *data, SomeObj *obj, const char *param0, const char *param1); There is another function which has a slightly different callback signature: typedef void (*callback2) (void *data, SomeObj *obj, void *extra); Now, I don’t care ..

Read more

Let’s say I have: template <typename…> class Foo { }; Then I define another function: template <typename… T> void Bar(const T&… arguments) { } How do I check if all Ts passed to Bar are all instantiated from Foo? Like: Foo<int> a; Foo<int, int> b; Bar(a, b); // OK int c; Bar(a, b, c); // ..

Read more

Thing simple enough, I want to forward the call of a member function, along with its arguments, as described in the following snippet. Please note that this is not a duplicate of this question, nor this one. #include <iostream> #include <functional> template<class Function, class … Args> auto forward_args(Function&& f, Args&& … args) { return f(std::forward<Args>(args)…); ..

Read more