C++-メンバー関数(member function)テンプレート(template)詳細とコード
3201 ワード
メンバー関数(member function)テンプレート(template)の詳細とコード
本住所:http://blog.csdn.net/caroline_wendy/article/details/16918085
メンバーテンプレート(member template)は、通常クラス(ordinary class)でもクラステンプレート(class template)でもよい.
一般クラスでは、メンバー関数を使用する場合、テンプレートパラメータを指定する必要はありません.関数は使用するパラメータに基づいて、
自動導出(deduce)テンプレート実パラメータ(template argument)対応テンプレート形パラメータ(template parameter);
クラステンプレートでは、メンバー関数のテンプレートパラメータ(template parameter)はクラスのテンプレートパラメータとは異なりますが、定義(definition)では、
2つのテンプレートパラメータリスト(template parameter list)を追加する必要があります.1つ目はクラスで、2つ目はメンバー関数です.
コードは次のとおりです.
出力:
本住所:http://blog.csdn.net/caroline_wendy/article/details/16918085
メンバーテンプレート(member template)は、通常クラス(ordinary class)でもクラステンプレート(class template)でもよい.
一般クラスでは、メンバー関数を使用する場合、テンプレートパラメータを指定する必要はありません.関数は使用するパラメータに基づいて、
自動導出(deduce)テンプレート実パラメータ(template argument)対応テンプレート形パラメータ(template parameter);
クラステンプレートでは、メンバー関数のテンプレートパラメータ(template parameter)はクラスのテンプレートパラメータとは異なりますが、定義(definition)では、
2つのテンプレートパラメータリスト(template parameter list)を追加する必要があります.1つ目はクラスで、2つ目はメンバー関数です.
コードは次のとおりです.
/* * cppprimer.cpp * * Created on: 2013.11.24 * Author: Caroline */ /*eclipse cdt, gcc 4.8.1*/ #include <iostream> #include <functional> #include <memory> #include <algorithm> #include <string> #include <vector> #include <deque> #include <iterator> // template <typename T, typename F = std::less<T>> int compare (const T &v1, const T &v2, F f = F()) { if (f(v1, v2)) return -1; if (f(v2, v1)) return 1; return 0; } class DebugDelete { public: DebugDelete (std::ostream &s = std::cerr) : os (s) { } template <typename T> void operator() (T *p) const { os << "deleting unique_ptr" << std::endl; delete p; } private: std::ostream &os; }; template <typename T> class Blob { public: template <typename It> Blob (It b, It e); /*template <typename It> Blob (It b, It e) { std::sort(b, e); }*/ }; template <typename T> template <typename It> Blob<T>::Blob (It b, It e) { std::sort(b, e); // } int main (void) { std::cout << "compare (0, 42) = " << compare (0, 42) << std::endl; double* p = new double; DebugDelete d; d(p); // , int* ip = new int; DebugDelete() (ip); std::unique_ptr<int, DebugDelete> pi (new int, DebugDelete()); std::unique_ptr<std::string, DebugDelete> ps (new std::string, DebugDelete()); int ia[] = {9, 8, 7, 6, 5}; std::vector<long> vi = {5, 4, 3, 2, 1, 0}; std::deque<std::string> w = {"lady", "girl", "woman", "now"}; Blob<int> a1(std::begin(ia), std::end(ia)); Blob<int> a2(vi.begin(), vi.end()); Blob<std::string> a3(w.begin(), w.end()); std::cout << "int ia[] = "; for (const auto i : ia) { std::cout << i << " "; } std::cout << std::endl; std::cout << "std::vector<long> vi = "; for (const auto i : vi) { std::cout << i << " "; } std::cout << std::endl; std::cout << "std::list<const char*> w = "; for (const auto i : w) { std::cout << i << " "; } std::cout << std::endl; return 0; }
出力:
deleting unique_ptr deleting unique_ptr deleting unique_ptr deleting unique_ptr compare (0, 42) = -1 int ia[] = 5 6 7 8 9 std::vector<long> vi = 0 1 2 3 4 5 std::list<const char*> w = girl lady now woman