詳解C++17中類模板參數(shù)推導的使用
在C++17之前,必須明確指出類模板的所有參數(shù)。自從C++17起必須指明類模板參數(shù)的限制被放寬了。通過使用類模板參數(shù)推導(Class Template Argument Deduction(CTAD)),只要編譯器能根據(jù)初始值推導出所有模板參數(shù),那么就可以不指明參數(shù)。
C++17中的類模板參數(shù)推導:允許從構造函數(shù)參數(shù)推導模板參數(shù),簡而言之,編譯器可以根據(jù)構造函數(shù)參數(shù)自動推導它們,而不是顯式指定模板參數(shù)。
用戶定義的推導指南(User-defined deduction guides):允許你為類模板參數(shù)推導提供自定義規(guī)則。這些指南幫助編譯器根據(jù)構造函數(shù)參數(shù)推斷模板參數(shù)。
別名模板的推導(deduction for alias templates):允許你根據(jù)別名模板的基礎類型或值(underlying type or value)推導別名模板的模板參數(shù)。
注意:
(1).推導過程中模板參數(shù)必須沒有歧義。
(2).推導模板參數(shù)時不會使用隱式類型轉換。
(3).僅當不存在模板參數(shù)列表時才執(zhí)行類模板參數(shù)推導。如果指定了模板參數(shù)列表,則不會發(fā)生推導。
(4).類模板參數(shù)推導過程中會首先嘗試以拷貝的方式初始化。
(5).類模板不能只指明一部分模板參數(shù),然后指望編譯器去推導剩余的部分參數(shù)。
(6).在任何情況下,對于像std::vector<>或其他STL容器一樣擁有復雜的構造函數(shù)的類模板,強烈建議不要使用類模板參數(shù)推導,而是顯式指明類型。
(7).智能指針沒有推導指引。
以下為測試代碼:
namespace { template<typename T> class Foo { public: Foo(T value) { std::cout << "Created Foo, type id: " << typeid(T).name() << ", value: " << value << "\n"; } }; template<typename T> class Foo2 { public: Foo2(T value) { std::cout << "Created Foo2, type id: " << typeid(T).name() << ", value: " << value << "\n"; } }; // User-defined deduction guide template<typename T> Foo2(T)->Foo2<T>; template<typename T> using MyAlias = T; template<typename T> class Foo3 { public: Foo3(const std::initializer_list<T>& list) : vec_(list) {} void print() const { for (const auto& val : vec_) { std::cout << val << " "; } std::cout << "\n"; } private: std::vector<T> vec_; }; } // namespace int test_ctad() { // 只要能根據(jù)初始值推導出所有模板參數(shù)就可以使用類模板參數(shù)推導 // 推導過程支持所有方式的初始化(只要保證初始化是有效的) std::pair p(2, 4.5); // deduces to std::pair<int, double> p(2, 4.5); std::tuple t(4, 3, 2.5); // same as auto t = std::make_tuple(4, 3, 2.5); std::complex c{ 5.1, 3.3 }; // std::complex<double> std::mutex mx; std::lock_guard lg{ mx }; // std::lock_guard<std::mutex> std::vector vec{ "hello", "world" }; // std::vector<const char*> std::vector v1{ 42 }; std::vector v2{ v1 }; // v2也是一個std::vector<int>,這是花括號初始化總是把列表中的參數(shù)作為元素這一規(guī)則的一個例外 // 然而,如果用多于一個元素的初值列來初始化的話,就會把傳入的參數(shù)作為元素并推導出其類型作為模板參數(shù) std::vector vv{ v1, v2 }; // vv是一個vector<vector<int>> // 1.Class Template Argument Deduction(CTAD) // not specifying the type of the template auto foo11 = Foo(88); // Created Foo, type id: int, value: 88 auto foo12 = Foo("Beijing"); // Created Foo, type id: char const * __ptr64, value: Beijing // specifying the type of the template Foo<int> foo21(88); // Created Foo, type id: int, value: 88 Foo<const char*> foo22("Beijing"); // Created Foo, type id: char const * __ptr64, value: Beijing // 2.User-defined deduction guides // not specifying the type of the template auto foo31 = Foo2(88); // Created Foo2, type id: int, value: 88 auto foo32 = Foo2("Beijing"); // Created Foo2, type id: char const * __ptr64, value: Beijing // 3.the deduction for alias templates MyAlias<int> alias1{ 88 }; MyAlias<std::string> alias2{ "Beijing" }; std::cout << "alias1: " << alias1 << "\n"; // alias1: 88 std::cout << "alias2: " << alias2 << "\n"; // alias2: Beijing // reference: https://www.geeksforgeeks.org/class-template-argument-deduction-in-cpp-17/ auto foo41 = Foo3({ 1, 2, 3, 4, 5 }); foo41.print(); // 1 2 3 4 5 return 0; }
執(zhí)行結果如下圖所示:
GitHub:https://github.com/fengbingchun/Messy_Test
到此這篇關于詳解C++17中類模板參數(shù)推導的使用的文章就介紹到這了,更多相關C++17類模板參數(shù)推導內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!