The partial customization of the class template refers to the type of the class template (T), but different kinds, such as the left value, the right value, and so on;
The partial customization of the class template, and the same as the class template customization, all require the same class name, the same parameters;
Custom formal parameters (parameter) are more matched than the original template (original template);
Class template is partially customized, but function template is not, function template can only be overloaded;
Class template Custom Members, class templates can be individually customized member types, so that different instantiation classes, using custom members;
Code (partial customization):
* * * CppPrimer.cpp * * Created on:2013.12.9 * author:caroline//*eclipse CDT, gcc 4.8.1*/
#include <iostream> #include <vector> #include <cstring> using namespace std;
Template<typename t> struct myclass{void print () {std::cout << "myclass!" << Std::endl;}
}; A partial customization of the class, the left value template<typename t> struct myclass<t&>{void print () {std::cout << "MyClass ! Lvalue "<< Std::endl;
}
}; Right value template<typename t> struct myclass<t&&>{void print () {std::cout << "myclass! Rvalue "<< Std::endl;
}
};
int main (void) {int I (1988);
int& ri = i; Myclass<decltype (1988) > MC;
Original version mc.print (); Myclass<decltype (RI) > MCL;
Left value version mcl.print (); Myclass<decltype (Std::move (i)) > MCR;
Right-value version mcr.print (); return 0; }
More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/cplus/
Output:
Myclass!
myclass! Lvalue
myclass! rvalue
Code (custom member):
* * * CppPrimer.cpp * * Created on:2013.12.9 *
author:caroline/
/*eclipse CDT, GCC 4.8.1*/
#include <iostream>
#include <vector>
#include <cstring>
using namespace Std;
Template<typename t>
struct myclass{
void print () {std::cout << "myclass!" << Std::endl;}
};
The int version of the custom member
template<>
void myclass<int>::p rint ()
{
std::cout << "myclass! int "<< Std::endl;
}
int main (void)
{
myclass<double> mcd;
Mcd.print ();
MyClass <int> MCI;
Mci.print ();
return 0;
}
Output:
Myclass!
myclass! Int
Author: csdn Blog spike_king