Is it possible to write a function template which returns whether the number of arguments is divisible by N?

Yes, it’s as simple as template<int N, typename… Ts> constexpr bool number_of_args_divisible_by(Ts&&…) { return sizeof…(Ts) % N == 0; } Alternatively, you can return a more metaprogramming-friendly type: template<int N, typename… Ts> constexpr integral_constant<bool, sizeof…(Ts) % N == 0> number_of_args_divisible_by(Ts&&…) { return {}; }

recursive variadic template to print out the contents of a parameter pack

There’s actually a very elegant way to end the recursion: template <typename Last> std::string type_name () { return std::string(typeid(Last).name()); } template <typename First, typename Second, typename …Rest> std::string type_name () { return std::string(typeid(First).name()) + ” ” + type_name<Second, Rest…>(); } I initially tried template <typename Last> and template <typename First, typename …Rest> but that was … Read more

c++ lambdas how to capture variadic parameter pack from the upper scope

Perfect capture in C++20 template <typename … Args> auto f(Args&& … args){ return [… args = std::forward<Args>(args)]{ // use args }; } C++17 and C++14 workaround In C++17 we can use a workaround with tuples: template <typename … Args> auto f(Args&& … args){ return [args = std::make_tuple(std::forward<Args>(args) …)]()mutable{ return std::apply([](auto&& … args){ // use args … Read more

Variadic template templates and perfect forwarding

That’s exactly right. I would expect it to work. So I think that GCC is in error with rejecting that. FWIW: #include <utility> template <template <typename…> class TemplateClass, typename… Args> TemplateClass<Args…> make(Args&&… args) { return TemplateClass<Args…>(std::forward<Args>(args)…); } int main() { make<std::pair>(1, 2); } // [js@HOST2 cpp]$ clang++ -std=c++0x main1.cpp // [js@HOST2 cpp]$

How to call a function on all variadic template args?

C++17 fold expression (f(args), …); If you call something that might return an object with overloaded comma operator use: ((void)f(args), …); Pre-C++17 solution The typical approach here is to use a dumb list-initializer and do the expansion inside it: { print(Args)… } Order of evaluation is guaranteed left-to-right in curly initialisers. But print returns void … Read more

How to use source_location in a variadic template function?

The first form can be made to work, by adding a deduction guide: template <typename… Ts> struct debug { debug(Ts&&… ts, const std::source_location& loc = std::source_location::current()); }; template <typename… Ts> debug(Ts&&…) -> debug<Ts…>; Test: int main() { debug(5, ‘A’, 3.14f, “foo”); } DEMO

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)