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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T05:54:16+00:00 2026-05-25T05:54:16+00:00

I’m trying to find a method to iterate over an a pack variadic template

  • 0

I’m trying to find a method to iterate over an a pack variadic template argument list.
Now as with all iterations, you need some sort of method of knowing how many arguments are in the packed list, and more importantly how to individually get data from a packed argument list.

The general idea is to iterate over the list, store all data of type int into a vector, store all data of type char* into a vector, and store all data of type float, into a vector. During this process there also needs to be a seperate vector that stores individual chars of what order the arguments went in. As an example, when you push_back(a_float), you’re also doing a push_back(‘f’) which is simply storing an individual char to know the order of the data. I could also use a std::string here and simply use +=. The vector was just used as an example.

Now the way the thing is designed is the function itself is constructed using a macro, despite the evil intentions, it’s required, as this is an experiment. So it’s literally impossible to use a recursive call, since the actual implementation that will house all this will be expanded at compile time; and you cannot recruse a macro.

Despite all possible attempts, I’m still stuck at figuring out how to actually do this. So instead I’m using a more convoluted method that involves constructing a type, and passing that type into the varadic template, expanding it inside a vector and then simply iterating that. However I do not want to have to call the function like:

foo(arg(1), arg(2.0f), arg("three");

So the real question is how can I do without such? To give you guys a better understanding of what the code is actually doing, I’ve pasted the optimistic approach that I’m currently using.

struct any {
  void do_i(int   e) { INT    = e; }
  void do_f(float e) { FLOAT  = e; }
  void do_s(char* e) { STRING = e; }

  int   INT;
  float FLOAT;
  char *STRING;
};


template<typename T> struct get        { T      operator()(const any& t) { return T();      } };
template<>           struct get<int>   { int    operator()(const any& t) { return t.INT;    } };
template<>           struct get<float> { float  operator()(const any& t) { return t.FLOAT;  } };
template<>           struct get<char*> { char*  operator()(const any& t) { return t.STRING; } };

#define def(name)                                  \
  template<typename... T>                          \
  auto name (T... argv) -> any {                   \
   std::initializer_list<any> argin = { argv... }; \
    std::vector<any> args = argin;
#define get(name,T)  get<T>()(args[name])
#define end }

any arg(int   a) { any arg; arg.INT    = a; return arg; }
any arg(float f) { any arg; arg.FLOAT  = f; return arg; }
any arg(char* s) { any arg; arg.STRING = s; return arg; }

I know this is nasty, however it’s a pure experiment, and will not be used in production code. It’s purely an idea. It could probably be done a better way. But an example of how you would use this system:

def(foo)
  int data = get(0, int);
  std::cout << data << std::endl;
end

looks a lot like python. it works too, but the only problem is how you call this function.
Heres a quick example:

foo(arg(1000));

I’m required to construct a new any type, which is highly aesthetic, but thats not to say those macros are not either. Aside the point, I just want to the option of doing:
foo(1000);

I know it can be done, I just need some sort of iteration method, or more importantly some std::get method for packed variadic template argument lists. Which I’m sure can be done.

Also to note, I’m well aware that this is not exactly type friendly, as I’m only supporting int,float,char* and thats okay with me. I’m not requiring anything else, and I’ll add checks to use type_traits to validate that the arguments passed are indeed the correct ones to produce a compile time error if data is incorrect. This is purely not an issue. I also don’t need support for anything other then these POD types.

It would be highly apprecaited if I could get some constructive help, opposed to arguments about my purely illogical and stupid use of macros and POD only types. I’m well aware of how fragile and broken the code is. This is merley an experiment, and I can later rectify issues with non-POD data, and make it more type-safe and useable.

Thanks for your undertstanding, and I’m looking forward to help.

  • 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-25T05:54:17+00:00Added an answer on May 25, 2026 at 5:54 am

    If you want to wrap arguments to any, you can use the following setup. I also made the any class a bit more usable, although it isn’t technically an any class.

    #include <vector>
    #include <iostream>
    
    struct any {
      enum type {Int, Float, String};
      any(int   e) { m_data.INT    = e; m_type = Int;}
      any(float e) { m_data.FLOAT  = e; m_type = Float;}
      any(char* e) { m_data.STRING = e; m_type = String;}
      type get_type() const { return m_type; }
      int get_int() const { return m_data.INT; }
      float get_float() const { return m_data.FLOAT; }
      char* get_string() const { return m_data.STRING; }
    private:
      type m_type;
      union {
        int   INT;
        float FLOAT;
        char *STRING;
      } m_data;
    };
    
    template <class ...Args>
    void foo_imp(const Args&... args)
    {
        std::vector<any> vec = {args...};
        for (unsigned i = 0; i < vec.size(); ++i) {
            switch (vec[i].get_type()) {
                case any::Int: std::cout << vec[i].get_int() << '\n'; break;
                case any::Float: std::cout << vec[i].get_float() << '\n'; break;
                case any::String: std::cout << vec[i].get_string() << '\n'; break;
            }
        }
    }
    
    template <class ...Args>
    void foo(Args... args)
    {
        foo_imp(any(args)...);  //pass each arg to any constructor, and call foo_imp with resulting any objects
    }
    
    int main()
    {
        char s[] = "Hello";
        foo(1, 3.4f, s);
    }
    

    It is however possible to write functions to access the nth argument in a variadic template function and to apply a function to each argument, which might be a better way of doing whatever you want to achieve.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I need a function that will clean a strings' special characters. I do NOT
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka

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.