DLL是Dynamic Link Library的缩写,意为动态链接库。在Windows中,许多应用程序并不是一个完整的可执行文件,它们被分割成一些相对独立的动态链接库,即DLL文件,放置于系统中。当我们执行某一个程序时,相应的DLL文件就会被调用。一个应用程序可有多个DLL文件,一个DLL文件也可能被几个应用程序所共用,这样的DLL文件被称为共享DLL文件。
voidTest10(){ Test10Class<int> myObj{25}; cout << "my val is:" << myObj.t << endl; }
成员模板
在函数内对成员函数定义单独的模板,注意不支持偏特化,必须全特化
classBoolString{ public: template<typename T = std::string> T get()const{ // get value (converted to T) return value; } template<> boolget<bool>() const { return value; } }
classPerson { private: std::string name; public: // generic constructor for passed initial name: template<typename STR> explicitPerson(STR&& n) : name(std::forward<STR>(n)) { std::cout << "TMPL-CONSTR for '" << name << "'\n"; } // copy and move constructor: Person (Person const& p) : name(p.name) { std::cout << "COPY-CONSTR Person '" << name << "'\n"; } Person (Person&& p) : name(std::move(p.name)) { std::cout << "MOVE-CONSTR Person '" << name << "'\n"; } };
std::string s = "sname"; Person p1(s); // init with string object => calls TMPL-CONSTR Person p2("tmp"); // init with string literal => calls TMPL-CONSTR Person p3(p1); // ERROR Person p4(std::move(p1)); Person constp2c("ctmp") Person p3c(p2c);
std::enable_if
C++11后提供了一种(删除代码)禁用模板的方法,可以在尖括号中用括号写入表达式,以结果为决定是否能用这个模板, C++ 14后添加一个enable_if_t的方法支持成功后返回第二个参数
template<typename T> classC { public: C() = default; // user-define the predefined copy constructor as deleted // (with conversion to volatile to enable better matches) C(C constvolatile&) = delete; // if T is no integral type, provide copy constructor template with better match template<typename U, typename = std::enable_if_t<!std::is_integral<U>::value>> C (C<U> const&) { cout << "Good" << endl; } };