Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8585407
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:03:19+00:00 2026-06-11T22:03:19+00:00

I have never used variadic templates myself, but think I could need them now.

  • 0

I have never used variadic templates myself, but think I could need them now. Suppose I have a class

class A {
  int Kern;
  template<int> void func_a(int, double) const;
  template<int> void func_b(double, double, char) const;
  template<int> unsigned func_c(float, std::vector<int> const&) const;
public
  /* ... */
  void FuncA(int, double) const;
  void FuncB(double, double, char) const;
  unsigned FuncC(float, std::vector<int> const&) const;
};

where the definitions of A::FuncA() etc. are all of the form

void A::FuncA(int i, double x) const
{
   switch(Kern) {
   case 1: return func_a<1>(i,x);
   case 2: return func_a<2>(i,x);
   case 3: return func_a<3>(i,x);
   /* ... */
   }
 }

I currently implement this switch with a C-macro

#define SwitchKernMacro(KERN,FUNC)   \
switch(KERN) {                       \
case 1: FUNC(1);                     \
case 2: FUNC(2);                     \
case 3: FUNC(3);                     \
/* ... */                            \
}

such that

void A::FuncA(int i, double x) const
{
#define FuncK(KERN) return func_a<KERN>(i,x);
  SwitchKernMacro(Kern,FuncK);
#undef FuncK
}

I like to avoid this C-macro in favour of a variadic template solution, such that the implementation of my functions becomes simply (or similar)

void A::FuncA(int i, double x) const
{ return SwitchKern(Kern,func_a,i,x); }    
void A::FuncB(double a, double b, char c) const
{ return SwitchKern(Kern,func_b,a,b,c); }
unsigned A::FuncC(float f, std::vector<int> const&v) const
{ return SwitchKern(Kern,func_c,f,v); }

How should the template SwitchKern look like?

EDIT

there seems to be some confusion about C++ templates and when they can be used. Suppose, I only have the following very simple functions

class A {
  int Kern;
  template int> void simple() const;
public:
  void Simple() const
  {
    switch(K) {
    case 1: return simple<1>();
    case 2: return simple<2>();
    case 3: return simple<3>();
    default: return simple<0>();
    }
  }
  /* ... */
};

then I can also implement A::Simple() via

class A {
  /* ... */
  template<int> friend struct simple_aux;
};

template<class T, template<int> class SimpleAux>
void Switch(int K, const T* a) {
  switch(K) {
  case 1: return SimpleAux<1>(a)();
  case 2: return SimpleAux<2>(a)();
  case 3: return SimpleAux<3>(a)();
  default: return SimpleAux<0>(a)();
  }
}

template<int k> struct simple_aux
{
  const A*const a;
  explicit simple_aux(const A*a__) : a(a__) {}
  void operator()() { return a->simple<k>(); }
};

void A::Simple() const
{ Switch<A,simple_aux>(K,this); }

However, this solution does not allow for return type different than void and for arbitrary arguments to the functions A::Simple() (passed to A::simple<>()). My question was how to add these functionalities using variadic templates

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T22:03:20+00:00Added an answer on June 11, 2026 at 10:03 pm

    The problem is that function templates can’t be passed to a template, only class templates. You can work around this with helper classes:

    template<template<int i> class Helper, typename... Args>
    auto SwitchKern(int Kern, const A &a, Args &&...args)
    -> decltype((a.*(Helper<0>::func()))(args...))
    {
        switch (Kern) {
        case 1: return (a.*(Helper<1>::func()))(std::forward<Args>(args)...);
        case 2: return (a.*(Helper<2>::func()))(std::forward<Args>(args)...);
        case 3: return (a.*(Helper<3>::func()))(std::forward<Args>(args)...);
        }
    }
    
    template<int i>
    struct FuncAHelper {
        static decltype(&A::func_a<i>) func() { return &A::func_a<i>; }
    };
    
    void A::FuncA(int i, double x) const
    {
        return SwitchKern<FuncAHelper, int &, double &>(Kern, *this, i, x);
    }
    

    See also Is there a generic way to adapt a function template to be a polymorphic function object?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have never used Try-catch in my code before, but now I need to
G'day, I have never really used excel formulas before but need to convert a
I have never used a Transaction, Commit and Rollback before and now I need
I have never used Prototype before. But now when I'm using Rails, it seems
I have never used ruby but need to translate this code to java. Can
I have never used Perl, but I am really impressed by the ack ,
I have never used DBIx::Class until today, so I'm completely new at it. I'm
I am new ASP.NET and I have never used a GridView or DataGrid, but
I have never used PHP with CLI, but I have seen scripts running with
I have never used Watin before today. I need to get a collection of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.