function template type deduction from default parameter

Discussion in 'C++' started by tobias.loew@steag.com, Jan 7, 2013.

  1. Guest

    Hi,

    I wanted my compiler (MSVC 9, VS2008) to deduce the template type of a function template from a default argument, but it doesn't work.

    template< class _dummy >
    void test( _dummy d = false )
    {
    ....
    }

    void foo()
    {
    test(); // <-- error C2783: "void test(_dummy)": could not deduce template argument for "_dummy"
    }


    So I looked into the standard (N3337) but I couldn't find anything specific for this case (neither at 14.8.2.1 nor at 8.3.5).

    Is there any reason, why this is not possible, or is it just an error in my compiler?

    regards

    Tobias
     
    , Jan 7, 2013
    #1
    1. Advertising

  2. On 1/7/2013 7:37 AM, wrote:
    > Hi,
    >
    > I wanted my compiler (MSVC 9, VS2008) to deduce the template type of a function template from a default argument, but it doesn't work.
    >
    > template< class _dummy >
    > void test( _dummy d = false )
    > {
    > ...
    > }
    >
    > void foo()
    > {
    > test(); // <-- error C2783: "void test(_dummy)": could not deduce template argument for "_dummy"
    > }
    >
    >
    > So I looked into the standard (N3337) but I couldn't find anything specific for this case (neither at 14.8.2.1 nor at 8.3.5).
    >
    > Is there any reason, why this is not possible, or is it just an error in my compiler?


    I have found one reference to the default argument values AFA template
    argument deduction is concerned. It's in the partial ordering section
    of the clause 14.

    14.8.2.4 Deducing template arguments during partial ordering
    [temp.deduct.partial]
    [...]
    3 The types used to determine the ordering depend on the context in
    which the partial ordering is done:
    — In the context of a function call, the types used are those function
    parameter types are used. for which
    the function call has arguments.<footnote 146> [...]
    ---
    146) Default arguments are not considered to be arguments in this
    context; they only become arguments after a function has
    been selected.

    I think there is a strong hint as to why 'bool' has no meaning in your
    case -- first the function has to be figured out, then its default
    arguments start playing the role, not before the function is actually found.

    BTW, unless this case is purely academic, theoretical, what problem were
    you solving that led you to this question? What is the reason that you
    have 'false' as the default, and not, say, '0'? Or, say, 'nullptr'?

    V
    --
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Jan 7, 2013
    #2
    1. Advertising

  3. Guest

    Thanks for the hint.
    The problem was the following:
    I wanted "test" to be an inline, non-template function in a header file, but then I had to put additional includes into the file for satisfying the stuff I used in test's body.
    So, I made it a template function to be weak bound, but I didn't want to change the function calls by adding angle-brackets or dummy arguments.
    As MSVC9 (i.e. C++ 03) doesn't allow default arguments for function templates, I tried it using the default argument.
     
    , Jan 8, 2013
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Neelesh

    Template parameter Deduction

    Neelesh, Nov 10, 2005, in forum: C++
    Replies:
    4
    Views:
    437
    John Carson
    Nov 10, 2005
  2. Fei Liu
    Replies:
    0
    Views:
    425
    Fei Liu
    Oct 25, 2007
  3. Fei Liu
    Replies:
    4
    Views:
    786
    Victor Bazarov
    Oct 26, 2007
  4. C++Liliput
    Replies:
    2
    Views:
    788
    James Kanze
    Oct 24, 2008
  5. er
    Replies:
    5
    Views:
    1,291
Loading...

Share This Page