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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:51:47+00:00 2026-05-27T05:51:47+00:00

Is it possible to write a c++ template function which takes a variable number

  • 0

Is it possible to write a c++ template function which takes a variable number of input variables of different types (number of input can be limited to say 10)?
For example take a function sql_exec() which executes an sql query string and saves the resulting rows in std vectors of the type supplied, i.e.

std::vector<double> x,y;
std::vector<std::string> s;
std::string query="select * from ...";

sql_exec(query, s,x,y); // error if less than 3 rows or conversion not possible

Now my naive approach would have been (limited to max 2 vectors)

struct null_type {};
template <typename T1=null_type, typename T2=null_type>
void sql_query(const std::string& query_str, std::vector<T1>& col1,
           std::vector<T2>& col2) {
    ...
}

Of course that’s stupid as I didn’t tell the function about default arguments and we get

error: default template arguments may not be used in function templates

but actually it compiles with gcc and -std=c++0x. However, obviously sql_query() still doesn’t take variable length input and needs to be called with 2 vectors. Also, I’d like to have something portable working on most of the current compilers. Anything obvious I’ve overlooked? I know I can change the design and maybe use boost::tuple or something else but I’d have liked such a simple interface.

  • 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-05-27T05:51:48+00:00Added an answer on May 27, 2026 at 5:51 am

    As said above, Boost.Preprocessor is the way to go if C++0x is not available, although it takes a while to get used to the syntax. The example below demonstrate the way Boost.Preprocessor can be used to define functions with variable (but limited) number of arguments.

    #include <boost/preprocessor/repetition.hpp>
    #include <boost/preprocessor/iteration/local.hpp>
    #include <boost/preprocessor/iteration/iterate.hpp>
    
    #define MAX_PARAMS  2
    
    class sql {
    public:
       // definition of the function in macro form
       #define SQL_QUERY_DEF(z, n, unused)                                     \
       template <BOOST_PP_ENUM_PARAMS(n, class T)>                             \
       void query(const std::string& query,                                    \
                BOOST_PP_ENUM_BINARY_PARAMS(n, const T, & x) );
    
       // does the actual code replication of SQL_QUERY_DEF
       #define BOOST_PP_LOCAL_MACRO(n)  SQL_QUERY_DEF(~, n, ~)
       #define BOOST_PP_LOCAL_LIMITS    (1, MAX_PARAMS)
       #include BOOST_PP_LOCAL_ITERATE()
    
       ...
    };
    
    
    // two helper functions:
    // expands to var0.clear(); var1.clear(); ...
    #define SQL_VECTOR_CLEAR(z,i,var) var##i.clear();
    // expands to var0.push_back(this->get_col<T0>(0); ...
    #define SQL_VECTOR_PUSH_BACK(z,i,var) var##i.push_back(this->get_col<T##i>(i));
    
    // definition of the function in macro form
    #define SQL_QUERY(z, n, unused)                                               \
    template <BOOST_PP_ENUM_PARAMS(n, class T)>                                   \
    void sql::query(const std::string& query,                                     \
                      BOOST_PP_ENUM_BINARY_PARAMS(n, std::vector< T,>& x) ){      \
       this->do_query(query);                                                     \
       if(this->num_cols()<n){                                                    \
          throw std::runtime_error();                                             \
       }                                                                          \
       BOOST_PP_REPEAT(n, SQL_VECTOR_CLEAR, x)                                    \
       while(this->is_open()) {                                                   \
          BOOST_PP_REPEAT(n, SQL_VECTOR_PUSH_BACK, x)                             \
          this->step();                                                           \
       }                                                                          \
    }
    
    // does the actual code replication of SQL_QUERY
    #define BOOST_PP_LOCAL_MACRO(n)  SQL_QUERY(~, n, ~)
    #define BOOST_PP_LOCAL_LIMITS    (1,  MAX_PARAMS)
    #include BOOST_PP_LOCAL_ITERATE()
    

    The preprocessor expands this to:

    $ g++ -P -E sql.cpp | astyle
    
    class sql {
    public:
       template < class T0> void query(const std::string& query, const T0 & x0 );
       template < class T0 , class T1> void query(const std::string& query, const T0 & x0 , const T1 & x1 );
       ...
    };
    template < class T0> void sql::query(const std::string& query, std::vector< T0 >& x0 ) {
       this->do_query(query);
       if(this->num_cols()<1) {
          throw std::runtime_error();
       }
       x0.clear();
       while(this->is_open()) {
          x0.push_back(this->get_col<T0>(0));
          this->step();
       }
    }
    template < class T0 , class T1> void sql::query(const std::string& query, std::vector< T0 >& x0 , std::vector< T1 >& x1 ) {
       this->do_query(query);
       if(this->num_cols()<2) {
          throw std::runtime_error();
       }
       x0.clear();
       x1.clear();
       while(this->is_open()) {
          x0.push_back(this->get_col<T0>(0));
          x1.push_back(this->get_col<T1>(1));
          this->step();
       }
    }
    

    Note, here we can’t use BOOST_PP_REPEAT(MAX_PARAMS, SQL_QUERY, ~) as it starts replication with 0 parameters but we need to start with 1, that’s why BOOST_PP_LOCAL_ITERATE() is needed which is more flexible.

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

Sidebar

Related Questions

I'd like to write a templated function which changes its behavior depending on template
Is it possible to somehow forbid using templated function for types for which specialization
Possible Duplicate: C++ Restrict Template Function Is it possible to write a C++ template
Is it possible to write a template that changes behavior depending on if a
Possible Duplicate: C++ HTML template framework, templatizing library, HTML generator library Planning to write
I'm trying to write a function that can call a webmethod from a webserive
Like the title tells: Is it possible to write a PHP-Function in a XSL-Document
I was wondering if it is possible to store function variables in memcached. I
Possible Duplicate: Is it possible to write a C++ template to check for a
how do I write a function pointer as default template argument, I'am guessing to

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.