variadic-templates

Does virtual inheritance force a base class to be default constructible?

让人想犯罪 __ 提交于 2021-02-07 13:16:33
问题 In the following code, the compiler is requesting the base class X to be default constructible . However, if I remove the virtual keyword from the inheritance of the class Node , the access to the member m_x becomes, of course, ambiguous, but the default constructor for class X is no longer required. What is the reason for that? #include <iostream> struct Apply { template< typename T > struct Node : virtual T // this line contains the virtual inheritance { template< typename ...Args> Node(

Calling a stateless lambda without an instance (only type)

房东的猫 提交于 2021-02-07 08:01:49
问题 I'm trying to write a wrapper for a "register callback" type of interface from a C library. The issue is quite complicated by the fact that, the library lets you register "variadic" functions by accepting a list of parameter definitions. Then at callback time, the function is expected to extract its arguments from a type-erased list of arguments. Good old C... The interface I'm trying to create is to accept any function, even a lambda, and automatically generate all the machinery to correctly

Calling a stateless lambda without an instance (only type)

☆樱花仙子☆ 提交于 2021-02-07 08:01:36
问题 I'm trying to write a wrapper for a "register callback" type of interface from a C library. The issue is quite complicated by the fact that, the library lets you register "variadic" functions by accepting a list of parameter definitions. Then at callback time, the function is expected to extract its arguments from a type-erased list of arguments. Good old C... The interface I'm trying to create is to accept any function, even a lambda, and automatically generate all the machinery to correctly

Can we use variadic template function to filter parameters of specific type, then pass the rest to another function?

两盒软妹~` 提交于 2021-02-07 05:57:06
问题 for example // we have a variadic function void print(...); // I need such a function to filter parameters of specific type template<typename... Args> void print_filter(const Args&... args) { // filter non-integral type print(integral args); } // my goal, all non-integral type can be ignored: print_filter(1.0, 2, "abc", 3) == print(2, 3) I have used up my knowledge to do that... can you help? or just to prove it's impossible, which also be very helpful. Thanks 回答1: A neat trick is to convert

Checking if variadic template parameters are unique using fold expressions

为君一笑 提交于 2021-02-07 05:49:05
问题 Given a variadic template parameter pack, I want to check if all types given to it are unique using an inline constexpr bool and fold expressions. I trie something like this: template<class... T> inline static constexpr bool is_unique = (... && (!is_one_of<T, ...>)); Where is_one_of is a similar bool that works correctly. But this line doesn't compile regardless of what I put into is_one_of. Can this even be done using fold expressions, or do I need to use a regular struct for this purpose?

Using fold expressions to print all variadic arguments with newlines inbetween

落花浮王杯 提交于 2021-02-07 05:25:07
问题 The classic example for C++17 fold expressions is printing all arguments: template<typename ... Args> void print(Args ... args) { (cout << ... << args); } Example: print("Hello", 12, 234.3, complex<float>{12.3f, 32.8f}); Output: Hello12234.3(12.3,32.8) I'd like to add newlines to my output. However, I can't find a good way to do that, the best I've found so far: template<typename ... Args> void print(Args ... args) { (cout << ... << ((std::ostringstream{} << args << "\n").str())); } This

Filter the types of a parameter pack

旧街凉风 提交于 2021-02-06 11:25:25
问题 I'd like to know if it's possible to filter the types passed to a variadic template (based on a predicate template) to produce another variadic template containing those types which satisfy the predicate: /** Filter a parameter pack */ template <template <class> class, template <class...> class, class...> struct filter; template <template <class> class Pred, template <class...> class Variadic> struct filter<Pred, Variadic> : Variadic<> {}; template <template <class> class Pred, template

Filter the types of a parameter pack

爱⌒轻易说出口 提交于 2021-02-06 11:24:39
问题 I'd like to know if it's possible to filter the types passed to a variadic template (based on a predicate template) to produce another variadic template containing those types which satisfy the predicate: /** Filter a parameter pack */ template <template <class> class, template <class...> class, class...> struct filter; template <template <class> class Pred, template <class...> class Variadic> struct filter<Pred, Variadic> : Variadic<> {}; template <template <class> class Pred, template

Filter the types of a parameter pack

孤街浪徒 提交于 2021-02-06 11:23:33
问题 I'd like to know if it's possible to filter the types passed to a variadic template (based on a predicate template) to produce another variadic template containing those types which satisfy the predicate: /** Filter a parameter pack */ template <template <class> class, template <class...> class, class...> struct filter; template <template <class> class Pred, template <class...> class Variadic> struct filter<Pred, Variadic> : Variadic<> {}; template <template <class> class Pred, template

Add all parameters with parameter pack expansion [duplicate]

て烟熏妆下的殇ゞ 提交于 2021-02-06 04:44:40
问题 This question already has answers here : How to call a function on all variadic template args? (3 answers) Closed 6 years ago . Consider I have a variadic template with int... parameters. For example a function like this: template<int... t> int add(){ return t... + ??? } All the method should do is adding all the parameters. It can be easily achieved using recursive variadic templates. However, is it also possible expressing this (or something similar like using other binary operators to