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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T10:27:31+00:00 2026-06-01T10:27:31+00:00

So, I was trying to write a function like this: void append_to_stream(std::ostream &stream) {

  • 0

So, I was trying to write a function like this:

void append_to_stream(std::ostream &stream)
{ }

template <typename T, typename... Args>
void append_to_stream(std::ostream &stream, T first, Args&&... rest)
{
  stream << first;
  append_to_stream(stream, rest...);
}

and call it like:

append_to_stream(stream, 
                 std::endl,
                 std::endl);

But this doesn’t work. I get an error that says ‘too many arguments’ to the function. I’ve narrowed it down to the point I know that the std::endl is guilty – probably because it’s a function. I managed to ‘solve’ this by declaring a struct called endl and define the <<operator for it so that it simply calls std::endl. This works but doesn’t feel particularly good. Is it not possible to accept std::endl as a template argument? The function works for other types.

Edit: here’s the error:

src/log/sinks/file_sink.cpp:62:21: error: too many arguments to function ‘void log::sinks::append_to_stream(std::string&, Args&& ...) [with Args = {}, std::string = std::basic_string<char>]’

Update

Trying to get the compiler to deduce the correct template arguments @MooingDuck suggested that a function of the following form could be used:

  template<class e, class t, class a> 
  basic_ostream<e,t>&(*)(basic_ostream<e,t>&os) get_endl(basic_string<e,t,a>& s) 
  {
return std::endl<e,t>;
  }

However, this doesn’t compile.

Error:

src/log/sinks/file_sink.cpp:42:28: error: expected unqualified-id before ‘)’ token
src/log/sinks/file_sink.cpp:42:53: error: expected initializer before ‘get_endl’

Any ideas why? For the sake of compiling this, I’ve added using namespace std;

  • 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-01T10:27:32+00:00Added an answer on June 1, 2026 at 10:27 am

    std::endl is a template, not a function, and the compiler cannot resolve which endl to use.

    Try:

    append_to_stream(std::cout,
                 std::endl<char, std::char_traits<char>>,
                 std::endl<char, std::char_traits<char>>);
    

    Or, MooingDuck’s solution (corrected):

    template<class e, class t, class a> //string version
    std::basic_ostream<e, t>& (*get_endl(const std::basic_string<e, t, a>&))
        (std::basic_ostream<e, t>& )
    { return std::endl<e,t>; } 
    
    template<class e, class t> //stream version
    std::basic_ostream<e, t>& (*get_endl(const std::basic_ostream<e, t>&))
        (std::basic_ostream<e, t>& )
    { return std::endl<e,t>; }
    
    int main () {
      std::ostream& stream = std::cout;
      append_to_stream(stream,
                     get_endl(stream),
                     get_endl(stream));
    }
    

    Here is get_endl solution, simplified by C++11 decltype feature:

    template<class e, class t, class a> //string version
    auto get_endl(const std::basic_string<e, t, a>&)
      -> decltype(&std::endl<e,t>)
    { return std::endl<e,t>; }
    
    template<class e, class t> //stream version
    auto get_endl(const std::basic_ostream<e,t>&)
      -> decltype(&std::endl<e,t>)
    { return std::endl<e,t>; }
    
    int main () {
      std::ostream& stream = std::cout;
      append_to_stream(stream,
                     get_endl(stream),
                     get_endl(stream));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a size function like this: size(void *p,int size); Which would
I'm trying to write a calendar function like this function get_date($month, $year, $week, $day,
I'm trying to write a word to a file using this function: extern void
i am trying to write one function which return sql statement like function get_sql($name=0,$date_start=0,$date_end=0)
I'm trying to write a let function that allows me to do things like:
I'm trying to write a function that accepts a variable number of parameters like
I am trying to write a template function which will extract the value of
I'm trying to write a function to traverse a tree with depth first search.
I'm trying to overload the comma operator with a non-friend non-member function like this:
I am trying write a function that generates simulated data but if the simulated

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.