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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:33:15+00:00 2026-05-29T10:33:15+00:00

I don’t know if this can be achieved through variadic template, variadic marcos or

  • 0

I don’t know if this can be achieved through variadic template, variadic marcos or even maybe metaprogramming.

Basically I have a logging object like this:

LOG << "This is the value of variable X: " << varaibleX;

but I also want this to be able to use LOG like a function

LOG ( "This is the value of variable X: ", variableX);

but the number of argument can be varying. (assuming they call can be converted into streams)

I was looking at LOG ( ... ), but really not sure how to expand the arguments.

So let’s say someone wrote

LOG(X, Y, Z);

and I want to expand this into:

LOG << X << Y << Z;

Can this be done?

  • 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-29T10:33:15+00:00Added an answer on May 29, 2026 at 10:33 am

    This can be done with variadic templates like below. Since its not clear what your LOG object is, I omitted the code to actually call LOG(…), but you should be able to port this do what you need:

    #include <iostream>
    
    /**
     * A specialization to stream the last argument 
     * and terminate the recursion. 
     */
    template<typename Stream, typename Arg1>
    Stream & DoLog(Stream & stream, const Arg1 & arg1)
    {
       return (stream << arg1);
    }
    
    /** 
     * Recursive function to keep streaming the arguments 
     * one at a time until the last argument is reached and 
     * the specialization above is called. 
     */
    template<typename Stream, typename Arg1, typename... Args>
    Stream & DoLog(Stream & stream, const Arg1 & arg1, const Args&... args)
    {
       return DoLog((stream << arg1), args...);
    }
    
    int main()
    {
       DoLog(std::cout, "First ", 5, 6) << " Last" << std::endl;
    }
    

    You’ll need to compile this with c++0x support. With g++, this can be done using the –std=c++0x flag.

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

Sidebar

Related Questions

Don't know why but I can't find a solution to this. I have 3
Don't know if this has been answered before. Have custom routes to users. If
Don't know if I worded the question right, but basically what I want to
(Don't know if this is strictly on-topic, but I don't see any better Stack
Don't know if this has been asked before, so point me to another question
I don't know how better to describe this. So take this example. var a
I don't want my user to even try downloading something unless they have Wi-Fi
Don't overthink this - there's a very commonly used term and I ... have
Don't have much to say, just can get into the event handler. XAML: <Grid>
Don't know if this is the right place to ask this, but I will

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.