Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ Moderated > a compiler erro...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 2 Topic 9483 of 10094
Post > Topic >>

a compiler error in template function default argument value

by kuangye <kuangye19840410@[EMAIL PROTECTED] > Apr 10, 2008 at 03:14 PM

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;
}

-- 
      [ See http://www.gotw.ca/resources/clcm.htm
for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
 




 2 Posts in Topic:
a compiler error in template function default argument value
kuangye <kuangye198404  2008-04-10 15:14:32 
Re: a compiler error in template function default argument value
cpluslearn@[EMAIL PROTECT  2008-04-10 20:37:53 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sat Oct 11 18:31:42 CDT 2008.