Thread: Variadic Template Strangeness

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445

    Variadic Template Strangeness

    I have the following code:

    Code:
    #include <iostream>
    
    using namespace std;
    
    template<typename... Args>
    void ConsoleLog(Args... args)
    {
      std::initializer_list<int>{ (std::cout << std::forward<Args>(args), 0)... };
    }
    
    int main()
    {
      ConsoleLog("foo", 1, 3.14, false, std::endl);
      cout << "Hello, World!" << endl;
      return 0;
    }
    It fails, with the message:
    Code:
    main.cpp:13:46: error: too many arguments to function 'void ConsoleLog(Args...) [with Args = {}]'
    If I remove std::endl from the argument list, it compiles just fine.

    What rule is being violated by passing std::endl in this context?

    I get the same output on all versions of G++ that I've tried, including 6.1.1.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Apparently std::endl is a function template and the compiler has trouble choosing an instantiation!

    Typecasting it seems to fix the issue.
    Code:
    ConsoleLog("foo", 1, 3.14, false, static_cast<ostream&(*)(ostream&)>(endl));

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Might be easier for ConsoleLog itself to always write a newline and flush.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by laserlight View Post
    Might be easier for ConsoleLog itself to always write a newline and flush.
    That was my solution as well. It makes sense that logging to the console would always terminate with a proper line ending.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  5. #5
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    I didn't even think of forwarding arguments to std::cout...

    Also, after having used markdown for a bit in chatrooms, I'm almost too used to it now. I almost wrote `std::cout`lol.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Specializing a variadic template: C++ Primer ex. 16.64
    By frank67 in forum C++ Programming
    Replies: 1
    Last Post: 10-31-2015, 12:46 PM
  2. how to write variadic template for variadic function?
    By Dave11 in forum C++ Programming
    Replies: 4
    Last Post: 04-02-2015, 03:16 AM
  3. Variadic Template Basic Syntax
    By Grumpulus in forum C++ Programming
    Replies: 3
    Last Post: 08-01-2014, 12:39 PM
  4. variadic template string formatting
    By Elkvis in forum C++ Programming
    Replies: 0
    Last Post: 09-14-2011, 03:54 PM
  5. C++0x: Variadic template of the same type
    By EVOEx in forum C++ Programming
    Replies: 7
    Last Post: 03-31-2010, 01:32 PM

Tags for this Thread