a compiler error in template function default argument value

Discussion in 'C++' started by kuangye, Apr 10, 2008.

  1. kuangye

    kuangye Guest

    Hi, all.
    I encounter a compiler error in gcc 3.4.
    Is there anyone encounter the some situation. And why ???
    /////////////////////////////////////
    #include <iostream>
    using namespace std;


    #define INT_ID 1
    #define FLOAT_ID 2
    #define CHAR_ID 3

    template<int tid>
    struct Tid2T{};

    #define TID2T_HELPER(id, type)\
    template<>\
    struct Tid2T<id>{\
    typedef type TyT;\
    };

    TID2T_HELPER(INT_ID, int)
    TID2T_HELPER(FLOAT_ID, float)
    TID2T_HELPER(CHAR_ID, char)


    template<int tid>
    class TS{
    public:
    typename Tid2T<tid>::TyT m_i;
    TS(typename Tid2T<tid>::TyT val):TS_ID(tid){
    m_i = val;
    }
    const int TS_ID;
    };

    template<typename Elt>
    class TV;

    //
    //If i define the default value here, then the program will be ok.
    //
    template<typename Elt>
    void fn(const TV<Elt>& v1, const Elt& s1/*=Elt(0)*/);


    template<typename Elt>
    class TV{
    public:
    TV(const Elt& e):TV_TS_ID(e.TS_ID){
    }
    const int TV_TS_ID;
    };


    //
    //If i define the default value here, then the compiler error will
    appear
    //
    template<typename Elt>
    void fn(const TV<Elt>& v1, const Elt& s1 = Elt(0))
    {
    cout<<"TV_TS_ID="<<v1.TV_TS_ID<<" TS_ID="<<s1.TS_ID<<endl;
    }

    template<typename T>
    void tryfn()
    {
    T e1(1);
    TV<T> v1(e1);

    //the following calling is error
    //why???
    fn(v1);

    //the following calling is ok
    //fn(v1, e1);
    }


    int main()
    {
    tryfn< TS<INT_ID> >();
    tryfn< TS<FLOAT_ID> >();
    tryfn< TS<CHAR_ID> >();

    return 0;
    }
    kuangye, Apr 10, 2008
    #1
    1. Advertising

  2. kuangye

    kuangye Guest

    Re: a compiler error in template function default argument value

    Compiling source file(s)...
    1.cpp
    1.cpp: In function `void tryfn() [with T = TS<1>]':
    1.cpp:74: instantiated from here
    1.cpp:63: error: no matching function for call to `fn(TV<TS<1> >&)'
    1.cpp: In function `void tryfn() [with T = TS<2>]':
    1.cpp:75: instantiated from here
    1.cpp:63: error: no matching function for call to `fn(TV<TS<2> >&)'
    1.cpp: In function `void tryfn() [with T = TS<3>]':
    1.cpp:76: instantiated from here
    1.cpp:63: error: no matching function for call to `fn(TV<TS<3> >&)'
    kuangye, Apr 10, 2008
    #2
    1. Advertising

  3. kuangye

    Barry Guest

    Re: a compiler error in template function default argument value

    On 4ÔÂ10ÈÕ, ÉÏÎç9ʱ05·Ö, kuangye <> wrote:
    > Hi, all.
    > I encounter a compiler error in gcc 3.4.
    > Is there anyone encounter the some situation. And why ???
    > /////////////////////////////////////
    > #include <iostream>
    > using namespace std;
    >
    > #define INT_ID 1
    > #define FLOAT_ID 2
    > #define CHAR_ID 3
    >
    > template<int tid>
    > struct Tid2T{};
    >
    > #define TID2T_HELPER(id, type)\
    > template<>\
    > struct Tid2T<id>{\
    > typedef type TyT;\
    >
    > };
    >
    > TID2T_HELPER(INT_ID, int)
    > TID2T_HELPER(FLOAT_ID, float)
    > TID2T_HELPER(CHAR_ID, char)
    >
    > template<int tid>
    > class TS{
    > public:
    > typename Tid2T<tid>::TyT m_i;
    > TS(typename Tid2T<tid>::TyT val):TS_ID(tid){
    > m_i = val;
    > }
    > const int TS_ID;
    >
    > };
    >
    > template<typename Elt>
    > class TV;
    >
    > //
    > //If i define the default value here, then the program will be ok.
    > //
    > template<typename Elt>
    > void fn(const TV<Elt>& v1, const Elt& s1/*=Elt(0)*/);


    add default parameter in declaration
    NOT in your definition

    so

    template<typename Elt>
    void fn(const TV<Elt>& v1, const Elt& s1 = Elt(0));

    >
    > template<typename Elt>
    > class TV{
    > public:
    > TV(const Elt& e):TV_TS_ID(e.TS_ID){
    > }
    > const int TV_TS_ID;
    >
    > };
    >
    > //
    > //If i define the default value here, then the compiler error will
    > appear
    > //
    > template<typename Elt>
    > void fn(const TV<Elt>& v1, const Elt& s1 = Elt(0))
    > {
    > cout<<"TV_TS_ID="<<v1.TV_TS_ID<<" TS_ID="<<s1.TS_ID<<endl;
    >
    > }
    >


    remove default parameter here. (const Elt& s1 /* = Elt(0) */ as you
    did before)

    Actually, there's nothing special in this "templated" case.
    Barry, Apr 10, 2008
    #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. Jim West
    Replies:
    3
    Views:
    747
    Jim West
    Oct 7, 2004
  2. LaundroMat
    Replies:
    50
    Views:
    945
    Hendrik van Rooyen
    Oct 14, 2006
  3. Vijai Kalyan
    Replies:
    4
    Views:
    690
    Vijai Kalyan
    Nov 8, 2005
  4. nw
    Replies:
    0
    Views:
    304
  5. Replies:
    1
    Views:
    305
    Ryan Davis
    May 6, 2005
Loading...

Share This Page