ArticleDirectory
There are a lot of syntax related to the template, which cannot be listed one by one. Here, only a few simple common syntaxes are tested.
The syntax for the following templates was tested using Dev-CPP4991, VC ++ 6.0, and Intel C ++ 8.0, both devcpp and ICC passed the test completely, while VC ++ 6 was partially unable to pass the test.
1. static template members
Template <typename T> struct testclass {
Static int _ data;
};
Template <> int testclass <char >:_ DATA = 1;
Template <> int testclass <long >:_ DATA = 2;
Int main (void ){
Cout <boolalpha <(1 = testclass <char >:: _ DATA) <Endl;
Cout <boolalpha <(2 = testclass <long>: _ DATA) <Endl;
}
2. templated class is special
template struct testclass {
testclass () {cout <"I, O "
};
template struct testclass {< br>
testclass () {cout <"T *, T *"
};
template struct testclass {
testclass () {cout <"const T *, T * "
};
int main (void) {
testclass obj1;
testclass obj2;
testclass obj3;
}< br>
[note]: VC ++ 6 compilation fails.
3. function template partial order
template struct testclass {
void swap (testclass &) {cout <"swap ()"
};
template inline void swap (testclass & X, testclass & Y) {
X. swap (y);
}< br>
int main (void) {
testclass obj1;
testclass obj2;
swap (obj1, obj2 );
}< br>
[note]: VC ++ 6 compilation fails.
4. class member function Template
Struct testclass {
Template <class T> void mfun (const T & T ){
Cout <t <Endl;
}
Template <class T> operator T (){
Return T ();
}
};
Int main (void ){
Testclass OBJ;
OBJ. mfun (1 );
Int I = OBJ;
Cout <I <Endl;
}
[Note]: For the second member function template, VC ++ 6 runs abnormally.
5. Derivation of default template parameters
Template <class T> struct test {
T;
};
Template <Class I, Class O = test <I> struct testclass {
I B;
O C;
};
6. Non-type template parameters
The template <class T, int n> struct testclass {
T _ t;
Testclass (): _ T (n ){
}
};
Int main (void ){
Testclass <int, 1> obj1;
Testclass <int, 2> obj2;
}
7. Empty template parameters
Template <class T> struct testclass;
Template <class T> bool operator = (const testclass <t> &, const testclass <t> &){
Return false;
};
Template <class T> struct testclass {
Friend bool operator ==<>( const testclass &, const testclass &);
};
[Note]: VC ++ 6 compilation fails
8. Special Template
Template <class T> struct testclass {
};
Template <> struct testclass <int> {
};
9.
Template <template <class T> Class X>
Struct widget {
};
[Note]: VC ++ 6 compilation fails.
10. An example provided by [hpho]
Struct widget1 {
Template <typename T>
T Foo (){}
};
Template <template <class T> Class X>
Struct widget2 {
};
[Note]: VC ++ 6 compilation fails.
Posted on Zhou Xing reading (1853) Comments (28) EDIT favorites
Comment # Re: Test intel C ++ 8.0 support for templates Seven cats http://www.meta-comm.com/engineering/boost-regression/developer/algorithm-string.html
Re: test the template support level of Intel C ++ 8.0