logo
down
shadow

recursion in variadic template function of different argument types


recursion in variadic template function of different argument types

By : Rasmus
Date : November 20 2020, 03:01 PM
I wish this help you 1 Problems with your approach 1.1 Missing return in foo<1>
Make sure you understand how a return from a nested call works. Your foo<1> calls foo<0> which returns its (foo<0>'s) first argument back to foo<1>. But your foo<1> does not care about foo<0>'s return because it called foo<0> like this:
code :
else {
    foo<i-1>(args...);// `i-1` becomes `0`
}
main.cpp: In function ‘Arg foo(Arg, Args ...) [with int INDEX = 1; Arg = int; Args = {double, const char*}]’:
main.cpp:23:1: warning: control reaches end of non-void function [-Wreturn-type]


 }
 ^
else {
    return foo<i-1>(args...);// NOTE: type of return value depends on `foo<i-1>`
}
template<int i, class Arg, class... Args>
Arg foo(Arg, Args... args) {// <--------- NOTE: must return a value of type `Arg`
template<size_t i, class T0, class... Ts>
auto foo(T0 v0, Ts... vs) {
  static_assert(i < 1u + sizeof...(Ts));
  if constexpr(0u == i) return v0;// <------ NOTE: must be `if constexpr` (C++17)
  else return foo<i-1u>(vs...);
}
template<int i>
struct foo_impl {
  static_assert(i > 0, "the case `i == 0` requires a specialization");

  template<class T0, class... Ts>
  static auto get(T0, Ts... vs) {
    return foo_impl<i-1>::get(vs...);
  }
};

template<>
struct foo_impl<0> {
  template<class T0, class... Ts>
  static auto get(T0 v0, Ts...) {
    return v0;
  }
};

template<int i, class... Ts>
auto foo(Ts... vs) {
  static_assert(i >= 0 && i < sizeof...(Ts), "index range: [0, size)");
  return foo_impl<i>::get(vs...);// forward to "implementation"
}


Share : facebook icon twitter icon
How do I get the argument types of a function pointer in a variadic template class?

How do I get the argument types of a function pointer in a variadic template class?


By : 0sg
Date : March 29 2020, 07:55 AM
I wish this helpful for you You can write function_traits class as shown below, to discover the argument types, return type, and number of arguments:
code :
template<typename T> 
struct function_traits;  

template<typename R, typename ...Args> 
struct function_traits<std::function<R(Args...)>>
{
    static const size_t nargs = sizeof...(Args);

    typedef R result_type;

    template <size_t i>
    struct arg
    {
        typedef typename std::tuple_element<i, std::tuple<Args...>>::type type;
    };
};
struct R{};
struct A{};
struct B{};

int main()
{
   typedef std::function<R(A,B)> fun;

   std::cout << std::is_same<R, function_traits<fun>::result_type>::value << std::endl;
   std::cout << std::is_same<A, function_traits<fun>::arg<0>::type>::value << std::endl;
   std::cout << std::is_same<B, function_traits<fun>::arg<1>::type>::value << std::endl;
} 
Variadic template function with equal argument types

Variadic template function with equal argument types


By : Maciej Woźniak
Date : March 29 2020, 07:55 AM
wish helps you I would like to write a template function like so:
code :
    template <typename ... T>
    void f(const T & ... args)
    {
        std::array<typename std::common_type<T...>::type,
                   sizeof...(T)> arr = {args...};
    }
   template <typename ... T>
   void f(const T & ... args)
   {
        auto arr = std::experimental::make_array<void>(args...);
   }
Variadic template class: Is it possible to implement one unique member function per variadic template argument?

Variadic template class: Is it possible to implement one unique member function per variadic template argument?


By : Mikhail Mm
Date : March 29 2020, 07:55 AM
I hope this helps . You can't unpack the parameter pack across the body of the template definition as you were describing in the question, but you can use CRTP to assemble an class that inherits a hierarchy with templatized specializations for each of the type-parameters you supply:
code :
#include <iostream>

template<class L, class... R> struct X;

template<class L>
struct X<L> { void handle(L& i) { std::cout << i.f() << "\n"; } };

template<class L, class... R>
struct X : public X<L>, public X<R...> { using X<L>::handle; using X<R...>::handle; };

struct A1 {
    int f() { return 1; }
};

struct A2 {
    int f() { return 2; }
};

struct B {
    int f() { return 10; }
};

struct B1 : public B {
    int f() { return 11; }
};

struct B2 : public B1 {
    int f() { return 12; }
};

int main() {
    X<A1, A2> x1;
    A1 a1; A2 a2;
    x1.handle(a1);
    x1.handle(a2);

    X<B, B1, B2> x2;
    B b; B1 b1; B2 b2;
    x2.handle(b);
    x2.handle(b1);
    x2.handle(b2);
}
How to transform a variadic template argument to another types for calling another function?

How to transform a variadic template argument to another types for calling another function?


By : Manish Laungani
Date : March 29 2020, 07:55 AM
I wish did fix the issue.
Is there a way to achieve this, without expose transform function?
code :
template <typename... Args>
Wrapper f(Args&&... args)
 {
   static const auto tf = [](auto && value)
    { return Wrapper(std::forward<decltype(value)>(value)); };

   return f2( tf(std::forward<Args>(args)) ... );
 }
class fc
 {
   private:
      template <typename T>
      Wrapper transform (T && value)
       { return Wrapper(std::forward<T>(value)); }

   public:
      template <typename... Args>
      Wrapper operator() (Args&&... args)
       { return f2( transform(std::forward<Args>(args)) ... ); }
 };
fc()(1, "abc", 2.3f);
fc f0;

f0(1, "abc", 2.3f);
template multiple variadic inheritance with variadic argument types

template multiple variadic inheritance with variadic argument types


By : Dave Husson
Date : March 29 2020, 07:55 AM
around this issue The fundamental problem here is that there is no syntax for "squeezing" multiple variadic parameter packs out of a single parameter pack.
The usual approach in these kinds of situations is to use a std::tuple to wrap each individual parameter pack, and make a parameter pack out of those tuples:
code :
    ISignalStorage<foo, std::tuple<int, double>, std::tuple<double, int>> a;
#include <tuple>


template <class SignalDispatcherClass, class ... ArgTypes>
class ISignalMap
{
};

// Take a class, and a tuple. Give me an ISignalMap for the class, and
// what's in the tuple.

template<typename cl, typename tuple_t> struct tuple_expansion;

template<typename cl, typename ...tuple_types>
struct tuple_expansion<cl, std::tuple<tuple_types...>> {

    typedef ISignalMap<cl, tuple_types...> type;
};

// Syntactic sugar.    
template<typename cl, typename tuple_t>
using tuple_expansion_t=typename tuple_expansion<cl, tuple_t>::type;

// And a variadic parameter pack of tuples...

template <class SignalDispatcherClass, class ... ArgTypes>
class ISignalStorage : public tuple_expansion_t<SignalDispatcherClass,
                        ArgTypes>...
{
};

class foo;

void bar()
{

    // Note the syntax: pass each "inner" parameter pack wrapped into a
    // tuple.
    ISignalStorage<foo, std::tuple<int, double>, std::tuple<double, int>> a;

    ISignalMap<foo, int, double> &b=a;
    ISignalMap<foo, double, int> &c=a;
}
Related Posts Related Posts :
  • OpenGL - Uniform not being passed properly?
  • C++ Visual Studio "Non-standard syntax; use '&' to create a pointer to member"
  • Doxygen won't resolve reference to typedef in namespace
  • How to test for assert in debug build, but returned value in release?
  • use boost spirit parse int pair to vector
  • How #define works in Programming when define has value with operator?
  • C++: Passing objects by value to a member function of the same class
  • Confusion in C++ constructor
  • Implicit conversion operator doesn't kick in with operator overloading
  • From tuple of N elements to tuple of N/2 pairs
  • Operators Not Working With Class Objects?
  • GDAL Not Linking
  • C++ aligning characters to display neatly with any input implemented
  • c++, method to display content in hash table.Using double linked list
  • How to apply a function to a variadic argument list and cat them to a tuple
  • Doubly linked list c++ crashing after 3rd node
  • Transform a specific type of argument using templates
  • Function with recursion is causing stack overflow
  • working with multi-word typedefs
  • Calling std::to_string or std::to_wstring based on given typedef
  • Ensure a `std::vector` won't move its pointer
  • Appopriate Container that acts like normal static array?
  • Using enable_if to disable a template constructor of a template class
  • define implicit conversion from template class to primitive type based on other template
  • C++ class template, how to overload [ ] operator in a specific stuation?
  • What is the purpose of using an iterator in this function's return value?
  • C++ overriding the value of inherited static const variables
  • C++: what is special about punning character arrays according to the standard?
  • Producer consumer pthreads program not finishing
  • How do I start two thread teams with different thread functions at the same time?
  • OpenGL 3.3: GL_INVALID_OPERATION when calling glBindBuffer
  • Boost Karma generator for composition of classes
  • Can the storage of trivially copyable objects be safely reallocated with realloc?
  • Can I force a C++ class to be non-abstract?
  • The QPropertyAnimation is automatically deleted when using QParallelAnimationGroup?
  • Failing to specialize function for function pointer
  • Check if two (smart) pointers point to the same derived class
  • Getting an error when using delete[] cpp
  • QTWidgets QTableWidget crash with segfault
  • clang and clang++ with ASAN generate different output
  • How to map BYTE array as FILE * on Windows
  • Logic Error With Function to Manipulate Linked List
  • Expected primary-expression before numeric constant
  • Pre-Decrement Operator
  • C++: how to input values separated by comma(,)
  • Correct way to write operator+ using rvalues
  • How to ignore whitespace and punctuation?
  • Prevent duplicated code on different data types (uint16_t/ uint32_t)
  • Writing Custom FPrintF
  • How to read from keyboard a complete random text, filter and cast
  • SIGSEGV when trying to change string in structure C/C++
  • c++: tuple with tag-type access
  • C++ initializing pointer crashes application randomly?
  • Options to replace deprecated DirectSound
  • how pass TextField text from QString in C++?
  • Undefined Behaviour: value of array element changes implicitly/illogically
  • OpenGL texture format, create image/texture data for OpenGL
  • [Boost]::DI creating unique shared_ptr objects from injector
  • What is the effect of ordering if...else if statements by probability?
  • Valgrind error: Syscall param epoll_pwait(sigmask) points to unaddressable byte(s)
  • shadow
    Privacy Policy - Terms - Contact Us © voile276.org