Problem with default parameters in templated functions

Discussion in 'C++' started by Chris Jefferson, Mar 7, 2005.

  1. I came across an unusual problem recently.. I think that I've can see
    why it's done, but it's still annoying. I was just wondering if I should
    have this problem (it came up in g++ for me)

    Consider the following (very reduced!) piece of code.

    struct foo {};

    template<typename U>
    void bar(U u = foo())
    { }



    Now, lets try to use bar:
    bar(1); //works
    bar<foo>(); //works
    bar(); //"No matching function to bar()"

    It seems slightly strange that this doesn't work.. at the moment I'm
    having to fix this the "old fashioned way", by adding (after removing
    the default parameter definition for bar)

    void bar()
    { bar(foo()); }

    Which works fine.. I'm still not clear on why it shouldn't be able to
    perform the deduction however?

    Chris
     
    Chris Jefferson, Mar 7, 2005
    #1
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.