problem about "default template arguments may not be used in function templates "

Discussion in 'C++' started by sods, Dec 2, 2005.

  1. sods

    sods Guest

    Hi,

    I write a test code about template used for strategy.
    it's very similar to sample code in TC++PL 13.4.1.

    #include <iostream>
    #include <string>


    using std::basic_string;
    using std::string;
    using std::char_traits;
    using std::allocator;

    template<class T> class Cmp {
    public:
    static bool eq(const T& c1, const T& c2) { c1 == c2; }
    static bool lt(const T& c1, const T& c2) { c1 < c2; }
    };

    template< class Char, class T , class A , class C = Cmp<Char> >
    inline int compare(const basic_string<Char, T, A>& str1, const
    basic_string<Char, T, A>& str2) { // this compile error.
    for(int i=0; i<str1.length() && i<str2.length(); i++)
    if (!C::eq(str1, str2))
    return C::lt(str1, str2)?-1:1;
    return str1.length() - str2.length();
    }

    int main(void) {
    string s1 = "abcdefg";
    string s2 = "ABCDEFG";

    std::cout << compare(s1, s2) << std::endl;

    std::cin.get();
    }


    but when compiling, the diagnostic "default template arguments may not be
    used in function templates"
    occur in line commented out. why?

    thanks
    sods
    sods, Dec 2, 2005
    #1
    1. Advertising

  2. sods

    Guest

    Well, this is not the real problem with your code.
    with cl.exe (VC6) it says:
    warning C4519: default template arguments are only allowed on a class
    template; ignored
    Which means, you cannot define default arguments (i.e. "class C =
    Cmp<char>") in a function template (compare is a function template). It
    is only allowed in class templates.
    VC6 decides to ignores this... which OK until you are trying to use the
    function template.
    That's when you have a problem.
    When you try to use _complare_ on s1,s2 the compiler can deduce the
    type of Char, T and A, but not C (cause the default arguments are not
    allowed).
    And here is the error I get:
    error C2783: 'int __cdecl compare(const class
    std::basic_string<BasicChar,T,A> &,const class
    std::basic_string<BasicChar,T,A> &)' : could not deduce template
    argument for 'C'

    You can do instead, something like this:
    template< class BasicString, class C >
    inline int compare2(const BasicString& str1, const BasicString& str2)
    { // this compile error.
    for(int i=0; i<str1.length() && i<str2.length(); i++)
    if (!C::eq(str1, str2))
    return C::lt(str1, str2)?-1:1;
    return str1.length() - str2.length();
    }

    and the call:
    std::cout << compare2 <string, Cmp<char> >(s1, s2) << std::endl;

    can't think of something else right now...
    , Dec 4, 2005
    #2
    1. Advertising

  3. sods

    sods Guest

    Yeah, I think it's the cause.

    but my code is from the TC++PL(3rdEdition) ,
    13.4.1 Default Template Parameter, with less alter.

    it says:
    "Alternatively, we can supply the normal convention as a default template
    argument:
    template<class T, class C =Cmp<T> >
    int compare(const String<T>& str1, const String<T>& str2)
    {
    for(int i=0; i<str1.length() && i< str2.length() ; i++)
    if (!C: :eq(str1 ,str2)) return C: :lt(str1 ,str2) ? 1
    : 1;
    return str1.length()str2.
    length() ;
    }"
    and this code can't pass ,too.

    it's surprising , isn't it.

    sods


    <> дÈëÓʼþ
    news:...
    > Well, this is not the real problem with your code.
    > with cl.exe (VC6) it says:
    > warning C4519: default template arguments are only allowed on a class
    > template; ignored
    > Which means, you cannot define default arguments (i.e. "class C =
    > Cmp<char>") in a function template (compare is a function template). It
    > is only allowed in class templates.
    > VC6 decides to ignores this... which OK until you are trying to use the
    > function template.
    > That's when you have a problem.
    > When you try to use _complare_ on s1,s2 the compiler can deduce the
    > type of Char, T and A, but not C (cause the default arguments are not
    > allowed).
    > And here is the error I get:
    > error C2783: 'int __cdecl compare(const class
    > std::basic_string<BasicChar,T,A> &,const class
    > std::basic_string<BasicChar,T,A> &)' : could not deduce template
    > argument for 'C'
    >
    > You can do instead, something like this:
    > template< class BasicString, class C >
    > inline int compare2(const BasicString& str1, const BasicString& str2)
    > { // this compile error.
    > for(int i=0; i<str1.length() && i<str2.length(); i++)
    > if (!C::eq(str1, str2))
    > return C::lt(str1, str2)?-1:1;
    > return str1.length() - str2.length();
    > }
    >
    > and the call:
    > std::cout << compare2 <string, Cmp<char> >(s1, s2) << std::endl;
    >
    > can't think of something else right now...
    >
    sods, Dec 6, 2005
    #3
  4. * sods:
    > [top-posting]
    > [over-quoting]


    Please don't top-post in this group, or in any non-Microsoft Usenet
    group.

    Please don't quote extranous stuff, either.


    * sods:
    > * :
    > >
    > > with cl.exe (VC6) it says:
    > > warning C4519: default template arguments are only allowed on a class
    > > template; ignored

    >
    > Yeah, I think it's the cause.
    >
    > but my code is from the TC++PL(3rdEdition) ,
    > 13.4.1 Default Template Parameter, with less alter.
    >
    > it says:
    > "Alternatively, we can supply the normal convention as a default template
    > argument:
    > template<class T, class C =Cmp<T> >
    > int compare(const String<T>& str1, const String<T>& str2)


    <url: http://www.research.att.com/~bs/3rd_issues.html>
    "There are examples in my book that are in error according to the
    standard but that I haven't changed because there is a defect report on
    the issue."
    ....
    "*pg 340: Due to an unfortunate oversight, the standard simply bans
    default arguments for template parameters for a function template. Voted
    to be corrected in the next standard."

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Dec 6, 2005
    #4
  5. sods

    sods Guest

    "Alf P. Steinbach" <> дÈëÓʼþ
    news:...
    > * sods:
    > > [top-posting]
    > > [over-quoting]

    >
    > Please don't top-post in this group, or in any non-Microsoft Usenet
    > group.
    >
    > Please don't quote extranous stuff, either.
    >
    >
    > * sods:
    > > * :
    > > >
    > > > with cl.exe (VC6) it says:
    > > > warning C4519: default template arguments are only allowed on a class
    > > > template; ignored

    > >
    > > Yeah, I think it's the cause.
    > >
    > > but my code is from the TC++PL(3rdEdition) ,
    > > 13.4.1 Default Template Parameter, with less alter.
    > >
    > > it says:
    > > "Alternatively, we can supply the normal convention as a default

    template
    > > argument:
    > > template<class T, class C =Cmp<T> >
    > > int compare(const String<T>& str1, const String<T>& str2)

    >
    > <url: http://www.research.att.com/~bs/3rd_issues.html>
    > "There are examples in my book that are in error according to the
    > standard but that I haven't changed because there is a defect report on
    > the issue."
    > ...
    > "*pg 340: Due to an unfortunate oversight, the standard simply bans
    > default arguments for template parameters for a function template. Voted
    > to be corrected in the next standard."
    >
    > --
    > A: Because it messes up the order in which people normally read text.
    > Q: Why is it such a bad thing?
    > A: Top-posting.
    > Q: What is the most annoying thing on usenet and in e-mail?



    I'll take care. sorry for that.
    thanks for reminding.
    Also, thanks your reply. I got the key of problem.

    sods.
    sods, Dec 6, 2005
    #5
    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. Andreas Suurkuusk
    Replies:
    0
    Views:
    3,968
    Andreas Suurkuusk
    Jul 27, 2003
  2. Ted Miller
    Replies:
    0
    Views:
    5,147
    Ted Miller
    Sep 13, 2003
  3. Merek
    Replies:
    0
    Views:
    1,957
    Merek
    Dec 3, 2003
  4. Scott Meddows
    Replies:
    1
    Views:
    386
    John Saunders
    Jun 8, 2004
  5. Mike

    Problem problem problem :( Need Help

    Mike, May 7, 2004, in forum: ASP General
    Replies:
    2
    Views:
    544
    Bullschmidt
    May 11, 2004
Loading...

Share This Page