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 9123019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:14:21+00:00 2026-06-17T06:14:21+00:00

Say I have a template function that accepts variable arguments and calls other functions

  • 0

Say I have a template function that accepts variable arguments and calls other functions with those arguments…

template<typename... A> func(int i, A... args)
{
  // do something common

  switch (i)
  {
    case 0: x(args...); break;
    case 1: y(args...); break;
    case 2: z(args...); break;

    default: break; 
  }

  // do something common
}

…and the other functions are defined as…

void x(int a, int b);
void y(int a, int b);  // note: takes same args as x()
void z(std::string a);

This doesn’t compile because calling func() with a std::string argument will not find a match for x() and y() that takes a std::string argument.

Note that func() uses some external criteria (the value of parameter i in this example) to decide which other function to pass parameter args to.

Is there a way to pull this off without resorting to something like stuffing parameter args into a std:tuple and passing that?

[EDIT]

Ok, I figured out an approach that works well enough for my purposes. Here’s an example class to illustrate…

class X
{
  private:

    bool a(int) { return true; }
    bool b(int) { return true; }

    template<typename... A> bool a(const A&...) { return false; }
    template<typename... A> bool b(const A&...) { return false; }

  public:

    template<typename... A> void x(int i, const A&... args)
    {
        if (i == 0)
        {
            a(args...);
        }
        else
        {
            b(args...);
        }
    }
};

The variadic template methods for a() and b() will catch any calls made to X.x() where the arguments are not a single int. This resolves the compiler error I was receiving and enables the simpler class API that I was looking for.

  • 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-17T06:14:22+00:00Added an answer on June 17, 2026 at 6:14 am

    Yes, make separate overloads:

    template <typename A1>
    func(A1 arg1)
    {
        x(arg1);
    }
    
    template <typename A1, typename A2>
    func(A1 arg1, A2 arg2)
    {
        y(arg1, arg2);
    }
    
    template <typename A1, typename A2, typename A3>
    func(A1 arg1, A2 arg2, A3 arg3)
    {
        z(arg1, arg2, arg3);
    }
    

    (Any additional, common code can of course be factored into a separate, common function, possibly a variadic function template.)

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

Sidebar

Related Questions

If I have a template function, say one that calculates an average by summing
Say I have a template function in namespace A. I also have another namespace
Say I have an integer value that I output to a django template say
Lets say you have simple template function (not class member for the sake of
Say I have a templated Action template <class ArgT> struct Action { Action(::boost::function< void(ArgT)
I need a function template that accepts two iterators that could be pointers. If
Say I have a templated class: template <typename T> class foo { void do_someting(T
Let's say I have a controller template with a before function like so... public
Say I have : template < typename T > class ClassA { void doTheStuff
I have template functions, say, template<class T> void mysort(std::vector<T>& vec) { size_t b, m,

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.