Are default function parameters in allowed in function templates?

Discussion in 'C++' started by BRG, Jan 3, 2005.

  1. BRG

    BRG Guest

    I know that default template arguments cannot be used in function
    templates but are default function parameters legal?

    That is, is this:
    ----------------------------------
    #include <functional>

    template<class T, class C> bool count(T x[], C cmp = std::less<T>())
    {
    for(int i = 0; i < 9; ++i)
    if(cmp(, x[i + 1]))
    return true;
    return false;
    }
    ----------------------------------
    legal C++ code?

    count(x, std::less<int>()); // compiles ok, but
    count(x); // gives "could not deduce template argument for 'C'"

    Brian Gladman
    BRG, Jan 3, 2005
    #1
    1. Advertising

  2. BRG

    BRG Guest

    BRG wrote:
    > I know that default template arguments cannot be used in function
    > templates but are default function parameters legal?
    >
    > That is, is this:
    > ----------------------------------
    > #include <functional>
    >
    > template<class T, class C> bool count(T x[], C cmp = std::less<T>())
    > {
    > for(int i = 0; i < 9; ++i)
    > if(cmp(, x[i + 1]))
    > return true;
    > return false;
    > }
    > ----------------------------------
    > legal C++ code?
    >

    int x[10] // - I should have added this line as well
    > count(x, std::less<int>()); // compiles ok, but
    > count(x); // gives "could not deduce template argument for 'C'"


    Brian Gladman
    BRG, Jan 3, 2005
    #2
    1. Advertising

  3. BRG wrote:
    > I know that default template arguments cannot be used in function
    > templates but are default function parameters legal?
    > ...


    They are legal, but they won't work the way you expect them to work.
    Default arguments for function parameters are not used in template
    argument deduction process.

    --
    Best regards,
    Andrey Tarasevich
    Andrey Tarasevich, Jan 3, 2005
    #3
  4. BRG

    BRG Guest

    Andrey Tarasevich wrote:

    > BRG wrote:
    >
    >>I know that default template arguments cannot be used in function
    >>templates but are default function parameters legal?
    >>...

    >
    > They are legal, but they won't work the way you expect them to work.
    > Default arguments for function parameters are not used in template
    > argument deduction process


    Thanks for the explanation - but I am still puzzled. In:

    ------------------
    template<class T, class C> bool count(T x[], C cmp = std::less<T>())
    ....
    int x[10]
    count(x);
    ....
    ------------------

    the template argument T can be deduced to be 'int' from the first
    function parameter x. I hence don't see why deduction is involved in
    the second function parameter since this seems to be substitution (T =>
    int) rather than a deduction.

    I guess this is a quirk of C++. But it is a nuisance since I have to
    work around it.

    Thanks again for your help.

    Brian Gladman
    BRG, Jan 4, 2005
    #4
  5. On Tue, 04 Jan 2005 00:17:50 +0000, BRG <> wrote:
    > ------------------
    > template<class T, class C> bool count(T x[], C cmp = std::less<T>())
    > ...
    > int x[10]
    > count(x);


    i would try this:

    template<class T, class C = std::less<T> > bool count(T x[], C cmp = C());

    (template parameters can have default values (i.e. data types), and you
    avoid the possible type mismatch between the actual class passed in for
    template parameter C and the class std::less<T>)
    Ulrich Achleitner, Jan 4, 2005
    #5
  6. Ulrich Achleitner wrote:
    > On Tue, 04 Jan 2005 00:17:50 +0000, BRG <> wrote:
    >> ------------------
    >> template<class T, class C> bool count(T x[], C cmp = std::less<T>())
    >> ...
    >> int x[10]
    >> count(x);

    >
    > i would try this:
    >
    > template<class T, class C = std::less<T> > bool count(T x[], C cmp =
    > C());
    >
    > (template parameters can have default values (i.e. data types), and
    > you avoid the possible type mismatch between the actual class passed
    > in for template parameter C and the class std::less<T>)


    Actually, function template parameters currently cannot have defaults. This
    should be corrected in the next version of the standard. See
    http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#226.

    Jonathan
    Jonathan Turkanis, Jan 4, 2005
    #6
  7. Ulrich Achleitner, Jan 4, 2005
    #7
  8. BRG

    BRG Guest

    Jonathan Turkanis wrote:
    > Ulrich Achleitner wrote:
    >
    >>On Tue, 04 Jan 2005 00:17:50 +0000, BRG <> wrote:
    >>
    >>>------------------
    >>>template<class T, class C> bool count(T x[], C cmp = std::less<T>())
    >>>...
    >>>int x[10]
    >>>count(x);

    >>
    >>i would try this:
    >>
    >>template<class T, class C = std::less<T> > bool count(T x[], C cmp =
    >>C());
    >>
    >>(template parameters can have default values (i.e. data types), and
    >>you avoid the possible type mismatch between the actual class passed
    >>in for template parameter C and the class std::less<T>)

    >
    > Actually, function template parameters currently cannot have defaults. This
    > should be corrected in the next version of the standard. See
    > http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#226.


    Yes, that was my understanding too, which is why I was trying the other
    approach.

    Brian Gladman
    BRG, Jan 4, 2005
    #8
  9. On Tue, 04 Jan 2005 10:51:15 +0000, BRG <> wrote:

    >>>> template<class T, class C> bool count(T x[], C cmp = std::less<T>())


    this is never a correct place for a default _type_. in a function's
    parameter list there can only be default _values_.

    >>> i would try this:
    >>>
    >>> template<class T, class C = std::less<T> > bool count(T x[], C cmp =
    >>> C());

    >> Actually, function template parameters currently cannot have defaults.


    so, a valid version would be:
    template<class T, class C> bool count(T x[], C cmp = C());

    > Yes, that was my understanding too, which is why I was trying the other
    > approach.


    more you will not get with a template function, it seems.
    Ulrich Achleitner, Jan 4, 2005
    #9
  10. BRG

    BRG Guest

    Ulrich Achleitner wrote:
    > On Tue, 04 Jan 2005 10:51:15 +0000, BRG <> wrote:
    >
    >>>>> template<class T, class C> bool count(T x[], C cmp = std::less<T>())

    >
    > this is never a correct place for a default _type_. in a function's
    > parameter list there can only be default _values_.


    Yes, but std::less<T> and std::less<T>() are values, not types, aren't they?

    I thought that these are actual instances (or components) of a type (a
    structure) defined in <functional>.

    >>>> i would try this:
    >>>>
    >>>> template<class T, class C = std::less<T> > bool count(T x[], C cmp =
    >>>> C());
    >>>
    >>> Actually, function template parameters currently cannot have defaults.

    >
    > so, a valid version would be:
    > template<class T, class C> bool count(T x[], C cmp = C());


    I'll try this, thanks.

    >> Yes, that was my understanding too, which is why I was trying the
    >> other approach.

    >
    > more you will not get with a template function, it seems.


    At least until the new standard arrives :)

    Brian Gladman
    BRG, Jan 4, 2005
    #10
  11. On Tue, 04 Jan 2005 13:06:28 +0000, BRG <> wrote:

    >>>>>> template<class T, class C> bool count(T x[], C cmp = std::less<T>())

    >> this is never a correct place for a default _type_. in a function's
    >> parameter list there can only be default _values_.

    >
    > Yes, but std::less<T> and std::less<T>() are values, not types, aren't
    > they?


    the former is a type, the latter is a value / object. sorry, i overlooked
    the () in your original posting...
    Ulrich Achleitner, Jan 4, 2005
    #11
  12. BRG

    BRG Guest

    Ulrich Achleitner wrote:
    > On Tue, 04 Jan 2005 13:06:28 +0000, BRG <> wrote:
    >
    >>>>>>> template<class T, class C> bool count(T x[], C cmp = std::less<T>())
    >>>
    >>> this is never a correct place for a default _type_. in a
    >>> function's parameter list there can only be default _values_.

    >>
    >>
    >> Yes, but std::less<T> and std::less<T>() are values, not types,
    >> aren't they?

    >
    > the former is a type, the latter is a value / object. sorry, i
    > overlooked the () in your original posting...


    Thanks for taking an interest in this.

    I have had to abandon defaults here as I cannot find a way of making
    them work. What has worked as a way of inling the comparison code (to
    avoid the overhead of a function call) is:

    template<class T> inline bool
    lt_f(const T& x, const T& y) { return x < y; }

    template<class T, bool C(const T&, const T&)>
    bool count(T x[]) { ... C(x[j], x) ... }

    ...
    int q[10];
    count<int, lt_f<int> >(q);
    ...

    But I have had to build my own comparison templates in place of those in
    <functional>.

    Thanks again for your input.

    Brian Gladman
    BRG, Jan 4, 2005
    #12
    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. Michael Stembera

    Default Parameters in Nested Templates ?

    Michael Stembera, Nov 19, 2003, in forum: C++
    Replies:
    2
    Views:
    359
    Richard Smith
    Nov 21, 2003
  2. JKop
    Replies:
    3
    Views:
    455
  3. recover
    Replies:
    2
    Views:
    790
    recover
    Jul 25, 2006
  4. William Xu
    Replies:
    8
    Views:
    645
    Triple-DES
    Jun 3, 2008
  5. Iñaki Baz Castillo
    Replies:
    13
    Views:
    484
    Iñaki Baz Castillo
    May 1, 2011
Loading...

Share This Page