function templates doesn't support default template parameters?

Discussion in 'C++' started by William Xu, Jun 1, 2008.

  1. William Xu

    William Xu Guest

    Compiling:

    template <class T = int>
    T foo(const T& t)
    {}

    int main(int argc, char *argv[])
    {}

    gcc complains:

    ,----
    | /Users/william/repo/helloworlds/foo.cpp:2: error: default template
    | arguments may not be used in function templates
    `----

    But I find in "TC++PL(3rd, special edition)" P.340, Bjarne is giving
    function templates with default template parameters as examples.

    If current standard doesn't support it, what is the reason here?

    --
    William
     
    William Xu, Jun 1, 2008
    #1
    1. Advertising

  2. On 2008-06-01 16:29, William Xu wrote:
    > Compiling:
    >
    > template <class T = int>
    > T foo(const T& t)
    > {}
    >
    > int main(int argc, char *argv[])
    > {}
    >
    > gcc complains:
    >
    > ,----
    > | /Users/william/repo/helloworlds/foo.cpp:2: error: default template
    > | arguments may not be used in function templates
    > `----
    >
    > But I find in "TC++PL(3rd, special edition)" P.340, Bjarne is giving
    > function templates with default template parameters as examples.
    >
    > If current standard doesn't support it, what is the reason here?


    It is not supported in the current standard (14.1 §9), I do not know why.

    --
    Erik Wikström
     
    Erik Wikström, Jun 1, 2008
    #2
    1. Advertising

  3. William Xu

    Kai-Uwe Bux Guest

    Erik Wikström wrote:

    > On 2008-06-01 16:29, William Xu wrote:
    >> Compiling:
    >>
    >> template <class T = int>
    >> T foo(const T& t)
    >> {}
    >>
    >> int main(int argc, char *argv[])
    >> {}
    >>
    >> gcc complains:
    >>
    >> ,----
    >> | /Users/william/repo/helloworlds/foo.cpp:2: error: default template
    >> | arguments may not be used in function templates
    >> `----
    >>
    >> But I find in "TC++PL(3rd, special edition)" P.340, Bjarne is giving
    >> function templates with default template parameters as examples.
    >>
    >> If current standard doesn't support it, what is the reason here?

    >
    > It is not supported in the current standard (14.1 §9), I do not know why.


    I wonder what difference one should expect from

    template < typename T = int >
    T foo ( T const & arg );

    and

    template < typename T >
    T foo ( T const & arg );

    How would you use the default type? The type T would be deduced from the
    argument anyway, wouldn't it?


    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Jun 1, 2008
    #3
  4. William Xu

    William Xu Guest

    Kai-Uwe Bux <> writes:

    > How would you use the default type? The type T would be deduced from the
    > argument anyway, wouldn't it?


    Consider the following example then. What if I want the default
    comparision method to be Case_insensitive?

    #include <iostream>

    struct Case_insensitive
    {
    static bool eq(char c1, char c2) { return tolower(c1) == tolower(c2); }
    };

    struct Case_sensitive
    {
    static bool eq(char c1, char c2) { return c1 == c2; }
    };

    template <class Cmp>
    bool eq(char c1, char c2)
    {
    return Cmp::eq(c1, c2);
    }

    int main(int argc, char *argv[])
    {
    char c1 = 'h', c2 = 'H';

    /* These are okay. */
    eq<Case_insensitive>(c1, c2) ;
    eq<Case_sensitive>(c1, c2);

    /* But how about this one ? */
    /* eq(c1, c2); */

    }

    --
    William

    http://williamxu.net9.org

    You know what they say -- the sweetest word in the English language is revenge.
    -- Peter Beard
     
    William Xu, Jun 1, 2008
    #4
  5. William Xu

    Kai-Uwe Bux Guest

    William Xu wrote:

    > Kai-Uwe Bux <> writes:
    >
    >> How would you use the default type? The type T would be deduced from the
    >> argument anyway, wouldn't it?

    >
    > Consider the following example then. What if I want the default
    > comparision method to be Case_insensitive?
    >
    > #include <iostream>
    >
    > struct Case_insensitive
    > {
    > static bool eq(char c1, char c2) { return tolower(c1) == tolower(c2); }
    > };
    >
    > struct Case_sensitive
    > {
    > static bool eq(char c1, char c2) { return c1 == c2; }
    > };
    >
    > template <class Cmp>
    > bool eq(char c1, char c2)
    > {
    > return Cmp::eq(c1, c2);
    > }
    >
    > int main(int argc, char *argv[])
    > {
    > char c1 = 'h', c2 = 'H';
    >
    > /* These are okay. */
    > eq<Case_insensitive>(c1, c2) ;
    > eq<Case_sensitive>(c1, c2);
    >
    > /* But how about this one ? */
    > /* eq(c1, c2); */
    >
    > }


    Now, I can see your point.

    On the other hand, it looks as though you want a default argument not a
    default type:

    #include <cctype>

    bool case_insensitive ( char c1, char c2 ) {
    return ( std::tolower(c1) == std::tolower(c2) );
    }

    bool case_sensitive ( char c1, char c2 ) {
    return ( c1 == c2 );
    }

    typedef bool(* char_compare )(char,char);

    bool eq ( char c1, char c2, char_compare comp = &case_sensitive ) {
    return ( comp( c1, c2 ) );
    }

    int main ( void ) {
    eq( 'c', 'h' );
    }


    Then again, I am not at all sure whether it is a good idea to have a default
    in this case. I generally do not like magic hiding somewhere. Sooner or
    later it is going to bite you. From that point of view, I prefer the
    verbose version

    eq< case_sensitive >( c1, c2 );

    to

    eq( c1, c2 );



    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Jun 1, 2008
    #5
  6. William Xu

    James Kanze Guest

    On Jun 1, 4:46 pm, Erik Wikström <> wrote:
    > On 2008-06-01 16:29, William Xu wrote:
    > > Compiling:


    > > template <class T = int>
    > > T foo(const T& t)
    > > {}


    > > int main(int argc, char *argv[])
    > > {}


    > > gcc complains:


    > > But I find in "TC++PL(3rd, special edition)" P.340, Bjarne
    > > is giving function templates with default template
    > > parameters as examples.


    That surprises me a bit (but someone walked off with my copy of
    the 3rd edition, so I can't verify it).

    > > If current standard doesn't support it, what is the reason
    > > here?


    > It is not supported in the current standard (14.1 §9), I do
    > not know why.


    Probably because originally, function template arguments could
    only be deduced, not explicitly specified, and a defauld
    argument doesn't make sense in that case. For that matter,
    given the original poster's example, when would the default
    argument be used?

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Jun 1, 2008
    #6
  7. William Xu

    Eric Pruneau Guest

    "James Kanze" <> a écrit dans le message de news:
    ...
    On Jun 1, 4:46 pm, Erik Wikström <> wrote:
    > On 2008-06-01 16:29, William Xu wrote:
    > > Compiling:


    > > template <class T = int>
    > > T foo(const T& t)
    > > {}


    > > int main(int argc, char *argv[])
    > > {}


    > > gcc complains:


    > > But I find in "TC++PL(3rd, special edition)" P.340, Bjarne
    > > is giving function templates with default template
    > > parameters as examples.


    That surprises me a bit (but someone walked off with my copy of
    the 3rd edition, so I can't verify it).

    > > If current standard doesn't support it, what is the reason
    > > here?


    > It is not supported in the current standard (14.1 §9), I do
    > not know why.


    Probably because originally, function template arguments could
    only be deduced, not explicitly specified, and a defauld
    argument doesn't make sense in that case. For that matter,
    given the original poster's example, when would the default
    argument be used?

    --

    From C++ Templates (Vandevoorde, Josuttis)

    "When templates were originally added to the C++ language, explicit function
    template arguments were not a valid construct. [...] Since then, however, it
    is possible to specify explicitle function template arguments that cannot be
    deduced. "

    So the following should compile...

    template <typename T1, typename T2 = int>
    T2 count (T1 const& x);

    Cause T2 cannot be deduced since it is the return parameter. However I tried
    with intel c++ 9.1 and VS 2003 compilers and they give me an error...


    --------
    Eric Pruneau
     
    Eric Pruneau, Jun 2, 2008
    #7
  8. On 2008-06-02 03:43, Eric Pruneau wrote:
    > "James Kanze" <> a é–rit dans le message de news:
    > ...
    > On Jun 1, 4:46 pm, Erik Wikstré°‰ <> wrote:
    >> On 2008-06-01 16:29, William Xu wrote:
    >> > Compiling:

    >
    >> > template <class T = int>
    >> > T foo(const T& t)
    >> > {}

    >
    >> > int main(int argc, char *argv[])
    >> > {}

    >
    >> > gcc complains:

    >
    >> > But I find in "TC++PL(3rd, special edition)" P.340, Bjarne
    >> > is giving function templates with default template
    >> > parameters as examples.

    >
    > That surprises me a bit (but someone walked off with my copy of
    > the 3rd edition, so I can't verify it).
    >
    >> > If current standard doesn't support it, what is the reason
    >> > here?

    >
    >> It is not supported in the current standard (14.1 � I do
    >> not know why.

    >
    > Probably because originally, function template arguments could
    > only be deduced, not explicitly specified, and a defauld
    > argument doesn't make sense in that case. For that matter,
    > given the original poster's example, when would the default
    > argument be used?
    >
    > --
    >
    > From C++ Templates (Vandevoorde, Josuttis)
    >
    > "When templates were originally added to the C++ language, explicit function
    > template arguments were not a valid construct. [...] Since then, however, it
    > is possible to specify explicitle function template arguments that cannot be
    > deduced. "
    >
    > So the following should compile...
    >
    > template <typename T1, typename T2 = int>
    > T2 count (T1 const& x);
    >
    > Cause T2 cannot be deduced since it is the return parameter. However I tried
    > with intel c++ 9.1 and VS 2003 compilers and they give me an error...


    From the standard, 14.1 §9:

    A default template-argument is a template-argument (14.3) specified
    after = in a template-parameter. A default template-argument may be
    specified for any kind of template-parameter (type, non-type, template).
    A default template-argument may be specified in a class template
    declaration or a class template definition. A default template-argument
    shall not be specified in a function template declaration or a function
    template definition, nor in the template-parameter-list of the
    definition of a member of a class template. A default template-argument
    shall not be specified in a friend template declaration.

    --
    Erik Wikström
     
    Erik Wikström, Jun 2, 2008
    #8
  9. William Xu

    Triple-DES Guest

    On 2 Jun, 03:43, "Eric Pruneau" <> wrote:
    > From C++ Templates (Vandevoorde, Josuttis)
    >
    > "When templates were originally added to the C++ language, explicit function
    > template arguments were not a valid construct. [...] Since then, however, it
    > is possible to specify explicitle function template arguments that cannot be
    > deduced. "


    You actually snipped an important sentence here:
    "As a result, there seemed to be no compelling reason to allow default
    function template arguments because the default would always be
    overriden by the deduced value."

    >
    > So the following should compile...
    >
    > template <typename T1, typename T2 = int>
    > T2 count (T1 const& x);
    >
    > Cause T2 cannot be deduced since it is the return parameter. However I tried
    > with intel c++ 9.1 and VS 2003 compilers and they give me an error...


    The sentence I quoted above should make it clear that the examples in
    the book illustrate how default template arguments would be used if
    they were allowed, and are not well-formed c++.

    DP
     
    Triple-DES, Jun 3, 2008
    #9
    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.

Share This Page