variadic-templates

How to Write a Lambda Wrapping a Function with Optional Return Value

我只是一个虾纸丫 提交于 2021-01-05 11:46:45
问题 I have tried to write a lambda that measures the execution time of arbitrary functions. With a lot of help I have managed that for C++14 and functions having a return value, see Measure execution time of arbitrary functions with C++14 lambda. Then I wanted my code to also work with C++11, therefore I have implemented the same idea with template functions. Finally I have realized that this code does not work for functions having no return value. It has been quite simple to generalize the

How to avoid code duplicates with class template specializations

依然范特西╮ 提交于 2021-01-03 15:48:59
问题 I'd like to avoid the duplicates in the code below. #include <iostream> struct Bar{}; template <class... Args> struct FooClass; template <class... Args> inline void foo(Args&&... args) { FooClass<Args...>::impl(std::forward<Args>(args)...); } // Duplicate 1. // Const ref version template <> struct FooClass<Bar const&> { inline static void impl(const Bar& b) { std::cout << "dup1" << std::endl; } }; // Duplicate 2. // Copy version template <> struct FooClass<Bar> { inline static void impl(const

How to avoid code duplicates with class template specializations

让人想犯罪 __ 提交于 2021-01-03 15:48:09
问题 I'd like to avoid the duplicates in the code below. #include <iostream> struct Bar{}; template <class... Args> struct FooClass; template <class... Args> inline void foo(Args&&... args) { FooClass<Args...>::impl(std::forward<Args>(args)...); } // Duplicate 1. // Const ref version template <> struct FooClass<Bar const&> { inline static void impl(const Bar& b) { std::cout << "dup1" << std::endl; } }; // Duplicate 2. // Copy version template <> struct FooClass<Bar> { inline static void impl(const

Variadic template with template and non-type parameters

与世无争的帅哥 提交于 2020-12-10 06:22:33
问题 Having the code snippet below (under C++17 standard): template <class ...TParams> class Wrapper { public: template <template <class, TParams...> class TTemplate, class TValue, TParams ...params> class Base { public: TTemplate<TValue, params...> value; }; }; template <class T, bool Param1, int Param2> class Class { public: T value; bool boolean = Param1; int integer = Param2; }; template <class T, bool Param1, int Param2> class Derived :public Wrapper<bool, int>::Base<Class, T, Param1, Param2>

Variadic template with template and non-type parameters

╄→гoц情女王★ 提交于 2020-12-10 06:22:07
问题 Having the code snippet below (under C++17 standard): template <class ...TParams> class Wrapper { public: template <template <class, TParams...> class TTemplate, class TValue, TParams ...params> class Base { public: TTemplate<TValue, params...> value; }; }; template <class T, bool Param1, int Param2> class Class { public: T value; bool boolean = Param1; int integer = Param2; }; template <class T, bool Param1, int Param2> class Derived :public Wrapper<bool, int>::Base<Class, T, Param1, Param2>

Is there a way to specify all classes in a variadic parameter pack to be friend of the template in order to use operator=?

*爱你&永不变心* 提交于 2020-12-02 17:05:01
问题 I have seen a CRTP solution, which extracted the interface into the base class, and friended only one of the pack arguments per base class. Then the most derived class inherited all the friended base classes and implemented the interface. I cannot use this approach, since I need to protect the assignment operator, which is not inherited. Also, since the assignment operator has a defined signature with exactly one parameter, I cannot use a key pattern. This is what I would like to have:

Is there a way to specify all classes in a variadic parameter pack to be friend of the template in order to use operator=?

余生长醉 提交于 2020-12-02 17:04:19
问题 I have seen a CRTP solution, which extracted the interface into the base class, and friended only one of the pack arguments per base class. Then the most derived class inherited all the friended base classes and implemented the interface. I cannot use this approach, since I need to protect the assignment operator, which is not inherited. Also, since the assignment operator has a defined signature with exactly one parameter, I cannot use a key pattern. This is what I would like to have:

Is there a way to specify all classes in a variadic parameter pack to be friend of the template in order to use operator=?

心已入冬 提交于 2020-12-02 17:02:26
问题 I have seen a CRTP solution, which extracted the interface into the base class, and friended only one of the pack arguments per base class. Then the most derived class inherited all the friended base classes and implemented the interface. I cannot use this approach, since I need to protect the assignment operator, which is not inherited. Also, since the assignment operator has a defined signature with exactly one parameter, I cannot use a key pattern. This is what I would like to have: