锘??xml version="1.0" encoding="utf-8" standalone="yes"?> http://stackoverflow.com/questions/851584/default-template-class-argument-confuses-g 璺熸垜閬囧埌鐨勯棶棰樹竴鏍鳳紝緲昏瘧浜嗕竴灝忛儴鍒嗭紝浣滀負絎旇錛屽鏋滃摢涓湴鏂圭悊瑙i敊璇涓瀹氭寚鍑恒傝嫳璇ソ鐨勫彲浠ョ洿鎺ョ暐榪囪繖綃囨枃绔犱簡銆? 涓嶅緱涓嶆劅鍙規ā鏉跨紪紼嬮亣鍒扮殑闂鐪熼毦鐞嗚В銆? 鍏堝啓涓緥瀛愮▼搴忥細 濡傛灉Foo鏄崟妯℃澘鐨勮瘽錛屾病鏈夐棶棰樼殑銆?
緙栬瘧浜嗕笅錛岀湅涓嬭緭鍑猴細
$ g++-4.3 test.cpp
test.cpp:5: error: expected 鈥?鈥?or 鈥?..鈥?before 鈥?gt;鈥?token
test.cpp:5: error: wrong number of template arguments (1, should be 2)
test.cpp:2: error: provided for 鈥榯emplate<class A, class B> class Foo鈥?
test.cpp:5: error: default argument missing for parameter 2 of 鈥榲oid Bar::method(const Foo<int, int>&, int)鈥?
$ g++-4.4 test.cpp
$
鍙互鐪嬪埌g++4.3緙栬瘧閿欒錛実++4.4鍗寸紪璇戠殑榪囥?
鎺ヤ笅鏉ヤ負浜嗛槓榪拌繖涓棶棰橈紝鎴戠粺涓浣跨敤g++4.3鏉ョ紪璇戙?
緙栬瘧鍣ㄨ涓烘垜鎯寵鍐欒繖涔堜竴涓嚱鏁幫細 鍙緙栬瘧鍣ㄧ悊瑙i敊浜嗗彉閲忕被鍨嬶紝鐢╰ypedef鎸囧畾涓涓嬬湅鐪嬶紝淇敼涓婇潰鐨勪緥瀛愶細 緙栬瘧閫氳繃錛?
涓嶈繃鍒板簳鏄負浠涔堝憿錛?
鍏堢湅涓嬭繖涔堜慨鏀硅涓嶈錛?/p>
鍔犱簡鎷彿錛屽悓鏍峰彲浠~
涓轟簡璇存竻妤氾紝鍏堢湅涓涓嬩互涓嬬殑浠g爜錛?/p>
榪欐牱緙栬瘧鏄病鏈夐棶棰樼殑錛屽敖綆鏈0鏄庯紝浣嗙紪璇戝櫒鍙互鍒嗘瀽鍑簀鏉ャ?
鑰岀紪璇戝櫒鏄繖涔堢湅寰呮垜浠渶涓婇潰鐨勪唬鐮佺殑錛?/p>
榪欏氨涓嶉毦鐞嗚В緙栬瘧鍣ㄦ姤鐨勯敊璇簡錛屽鍔犱簡涓瀵規嫭鍙峰彲浠ヤ繚璇佺紪璇戝櫒鐞嗚В姝g‘銆?
鍚屾椂錛孴he following case shows an example where g++ succeeds but Comeau still generates a syntax error: 緇х畫寤朵幾涓涓嬶紝涓轟粈涔堝鍙傛暟鐨勫嚱鏁拌皟鐢ㄦ斁鍦ㄥ悓鏍風殑浣嶇疆涓嶄細鍑洪敊鍛紵姣斿錛?/p>
鍥犱負緙栬瘧鍣ㄦ槸榪欎箞鐞嗚В涓婇潰鐨勪唬鐮佺殑錛?/p>
涓轟粈涔堢紪璇戝櫒鍦ㄨ繖閲岀悊瑙e浜嗗憿錛熷叾瀹炰笉闅劇寽嫻嬶紝C++瑕佹眰鎵鏈夌殑鎷彿蹇呴』閰嶅錛屸?鈥欏氨鎰忓懗鐫鏈変竴涓?鈥欏湪鍚庨潰銆備絾妯℃澘鍙傛暟閲岀殑鈥?lt;鈥欎笉鍚岋紝C++鍏佽瀵光?lt;鈥欓噸杞姐?
The reason for the problem here is that '<' does not need to be matched, since it is overloaded in C++ and so can be the less than operator or the start of a template argument list.
鐪嬩笅榪欎釜渚嬪瓙灝辨槑鐧戒簡錛?/p>
緙栬瘧娌℃湁闂錛屽洜涓鴻繖閲屾病鏈変粈涔堥粯璁ゆā鏉垮弬鏁幫紝瀹為檯涓婃槸涓や釜鍙傛暟銆傘傘傘傘傘?
template<typename A,typename B>
class Foo {};
struct Bar {
void method( Foo<int,int> const &stuff = Foo<int,int>());
};
int main()
{
return 0;
}void Bar::method(const Foo<int, int>&, int)
struct Bar {
typedef Foo<int,int> FooType;
void method( FooType const &stuff = FooType());
};struct Bar {
void method( Foo<int,int> const &stuff = (Foo<int,int>()));
};struct Bar {
void method(int i = j);//鈥檍鈥?not declared yet
static const int j = 0;
};void method ( Foo<int,int> const& stuff = Foo<int // Arg 1 with dflt.
, int>() ); // Arg 2 - syntax errortemplate<int J, int K>
class Foo { };
struct Bar {
void method ( Foo<0, 0> const & i = ( Foo<j, k> () ) );
static const int j = 0;
static const int k = 0;
};int foo(int,int,int);
struct Bar {
void method(int i = foo(0,0,0));
};int foo (int, int, int);
struct Bar {
void method ( int j =
foo (0, 0, 0) ); // Comma's here are inside ( )
};template<int I, int J>
class Foo { };
struct Bar {
template <typename T> struct Y { };
void method ( ::Foo<0,0> const& stuff = Foo<10 , Y < int > = Y<int>() );
struct X {
::Foo<0, 0> operator< (int);
};
static X Foo;
};
#include <vector>
template<typename T>
class Foo {
};
template<typename T>
void method()
{
std::vector<Foo<T> > vecFoo;
std::vector<Foo<T> >::iterator iter = vecFoo.begin();
}
緙栬瘧鏄氫笉榪囩殑銆?
瑙e喅鍔炴硶錛?
typename std::vector<Foo<T> >::iterator iter = vecFoo.begin();
灝卞彲浠ヤ簡銆?
Effective C++:浜嗚Вtypename鐨勫弻閲嶆剰涔夐噷鍋氫簡瑙i噴銆?
闇瑕佺敤typename鍛婅瘔瑙f瀽鍣ㄨ繖鏄釜綾誨瀷錛屽惁鍒欑紪璇戝櫒浼氬綋鍋氱浉搴旂殑闈欐佸彉閲忔潵澶勭悊銆傛瘮濡?C鏄竴涓ā鏉跨被錛?
C::iterator* x;緙栬瘧鍣ㄤ細鐞嗚В涓烘槸C鐨勯潤鎬佸彉閲忎箻涓妜鈥︹?