variadic-templates

What's the essential difference between these two variadic functions?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-24 00:49:09
问题 I've been frustrated by a simple variadic template function: constexpr size_t num_args () { return 0; } template <typename H, typename... T> constexpr size_t num_args () { return 1 + num_args <T...> (); } int main () { std :: cout << num_args <int, int, int> (); } The above doesn't compile, see above linked question and followup for details, yet the following function DOES compile template <typename T, typename... Args> void foo (T, Args...); template <typename... Args> void foo (int, Args...

Getting the biggest type from a variadic type list

ぃ、小莉子 提交于 2019-12-24 00:38:14
问题 I'm trying to get the biggest type from a variadic template type list. I'm getting unexpected results: // Bigger between two types template<typename T1, typename T2> using Bigger = std::conditional_t<sizeof(T1) >= sizeof(T2), T1, T2>; // Recursion helper template<typename...> struct BiggestHelper; // 2 or more types template<typename T1, typename T2, typename... TArgs> struct BiggestHelper<T1, T2, TArgs...> { using Type = Bigger<T1, BiggestHelper<T2, TArgs...>>; }; // Exactly 2 types template

Meta-Function to get the first template of a list of templates

Deadly 提交于 2019-12-23 19:00:54
问题 Using the following metafunction front to get the first type of a typelist I try to write a similar metafunction to extract the first template of a list of templates. namescpace detail { template<typename L> struct front_impl; template<template<typename, typename...> typename L, typename F, typename... I> struct front_impl<L<F, I...>> { typedef F type; }; } template<typename L> struct front_impl; template<template<typename, typename...> typename L, typename F, typename... I> struct front_impl

Expanding an STL container into a variadic template

a 夏天 提交于 2019-12-23 18:13:40
问题 To keep things generic and straightforward, say that I have a std::vector of integers, such as: std::vector<int> v; Now, what I am wondering is, is it possible to take n (where n is a constant known at compile time) values from v and pass them to an arbitrary function? I know that this is doable with variadic templates: template<typename... T> void pass(void (*func)(int, int, int), T... t) { func(t...); } And then we hope 'pass' is called with exactly 3 integers. The details don't matter so

Unpack parameter pack into string view

 ̄綄美尐妖づ 提交于 2019-12-23 18:04:05
问题 It is possible to unpack a value template parameter pack of type char into a (compile time) string. How does one acquire a string_view into that string? What I want to do: int main() { constexpr auto s = stringify<'a', 'b', 'c'>(); constexpr std::string_view sv{ s.begin(), s.size() }; return 0; } Try: template<char ... chars> constexpr auto stringify() { std::array<char, sizeof...(chars)> array = { chars... }; return array; } Error: 15 : <source>:15:30: error: constexpr variable 'sv' must be

Syntax differences in variadic template parameter pack forwarding

巧了我就是萌 提交于 2019-12-23 15:36:39
问题 While working with variadic templates I have come across two different ways of writing a call to std::forward , but I am left wondering what the actual difference between the two syntaxes? template<typename... T> void one(T&&... args) { foo(std::forward<T&&...>(args...)); } template<typename... T> void two(T&&... args) { foo(std::forward<T&&>(args)...); } According to my compilers these are both valid syntax, and in most cases the compiler does not complain. But I have found some cases where

VS2013 fails with variadic template specialization

浪尽此生 提交于 2019-12-23 13:16:36
问题 This piece of code works fine with g++ and Clang: template <typename Sig, Sig& S> struct OpF; template <typename TR, typename ... Ts, TR (&f)(Ts...)> struct OpF<TR (Ts...), f> { }; int foo(int x) { return 0; } OpF<int (int), foo> f; But the new shiny VS2013 compiler bails out with f.cpp(4) : error C3520: 'Ts' : parameter pack must be expanded in this context Which one is at fault? 回答1: This was a bug in the VS2013 compiler, which seems to have been fixed now. For a workaround, see: Visual C++

VS2013 fails with variadic template specialization

被刻印的时光 ゝ 提交于 2019-12-23 13:16:34
问题 This piece of code works fine with g++ and Clang: template <typename Sig, Sig& S> struct OpF; template <typename TR, typename ... Ts, TR (&f)(Ts...)> struct OpF<TR (Ts...), f> { }; int foo(int x) { return 0; } OpF<int (int), foo> f; But the new shiny VS2013 compiler bails out with f.cpp(4) : error C3520: 'Ts' : parameter pack must be expanded in this context Which one is at fault? 回答1: This was a bug in the VS2013 compiler, which seems to have been fixed now. For a workaround, see: Visual C++

Ensure argument is an output stream for the console

折月煮酒 提交于 2019-12-23 12:47:13
问题 I'm trying to make a stream manipulator for colour for use with output to the console. It works, changing the colour of text and the background: std::cout << ConColor::Color::FgBlue << 123 << "abc"; //text is blue, sticky The problem is with the signature: std::ostream &FgBlue(std::ostream &); This signature allows for derived classes, such as std::ostringstream as well, but there is no way to change the colour of a string stream. The function would change the colour of the console regardless

Creating a sub-tuple starting from a std::tuple<some_types…>

◇◆丶佛笑我妖孽 提交于 2019-12-23 12:31:23
问题 Let us suppose that a std::tuple<some_types...> is given. I would like to create a new std::tuple whose types are the ones indexed in [0, sizeof...(some_types) - 2] . For instance, let's suppose that the starting tuple is std::tuple<int, double, bool> . I would like to obtain a sub-tuple defined as std::tuple<int, double> . I'm quite new to variadic templates. As a first step I tried to write a struct in charge of storing the different types of the original std::tuple with the aim of creating