网站开发虚拟主机管理系统,免费开网店平台有哪些,wordpress添加快速添加按钮,深圳外贸营销型网站建设在C17之前#xff0c;必须明确指出类模板的所有参数。自从C17起必须指明类模板参数的限制被放宽了。通过使用类模板参数推导(Class Template Argument Deduction(CTAD))#xff0c;只要编译器能根据初始值推导出所有模板参数#xff0c;那么就可以不指明参数。 C17中的类模板… 在C17之前必须明确指出类模板的所有参数。自从C17起必须指明类模板参数的限制被放宽了。通过使用类模板参数推导(Class Template Argument Deduction(CTAD))只要编译器能根据初始值推导出所有模板参数那么就可以不指明参数。 C17中的类模板参数推导:允许从构造函数参数推导模板参数简而言之编译器可以根据构造函数参数自动推导它们而不是显式指定模板参数。 用户定义的推导指南(User-defined deduction guides)允许你为类模板参数推导提供自定义规则。这些指南帮助编译器根据构造函数参数推断模板参数。 别名模板的推导(deduction for alias templates)允许你根据别名模板的基础类型或值(underlying type or value)推导别名模板的模板参数。 注意 (1).推导过程中模板参数必须没有歧义。 (2).推导模板参数时不会使用隐式类型转换。 (3).仅当不存在模板参数列表时才执行类模板参数推导。如果指定了模板参数列表则不会发生推导。 (4).类模板参数推导过程中会首先尝试以拷贝的方式初始化。 (5).类模板不能只指明一部分模板参数然后指望编译器去推导剩余的部分参数。 (6).在任何情况下对于像std::vector或其他STL容器一样拥有复杂的构造函数的类模板强烈建议不要使用类模板参数推导而是显式指明类型。 (7).智能指针没有推导指引。 以下为测试代码
namespace {templatetypename T
class Foo {
public:Foo(T value){std::cout Created Foo, type id: typeid(T).name() , value: value \n;}
};templatetypename T
class Foo2 {
public:Foo2(T value){std::cout Created Foo2, type id: typeid(T).name() , value: value \n;}
};// User-defined deduction guide
templatetypename T Foo2(T)-Foo2T;templatetypename T using MyAlias T;templatetypename T
class Foo3 {
public:Foo3(const std::initializer_listT list) : vec_(list) {}void print() const{for (const auto val : vec_) {std::cout val ;}std::cout \n;}private:std::vectorT vec_;
};} // namespaceint test_ctad()
{// 只要能根据初始值推导出所有模板参数就可以使用类模板参数推导// 推导过程支持所有方式的初始化(只要保证初始化是有效的)std::pair p(2, 4.5); // deduces to std::pairint, 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::complexdoublestd::mutex mx;std::lock_guard lg{ mx }; // std::lock_guardstd::mutexstd::vector vec{ hello, world }; // std::vectorconst char*std::vector v1{ 42 };std::vector v2{ v1 }; // v2也是一个std::vectorint,这是花括号初始化总是把列表中的参数作为元素这一规则的一个例外// 然而,如果用多于一个元素的初值列来初始化的话,就会把传入的参数作为元素并推导出其类型作为模板参数std::vector vv{ v1, v2 }; // vv是一个vectorvectorint// 1.Class Template Argument Deduction(CTAD)// not specifying the type of the templateauto foo11 Foo(88); // Created Foo, type id: int, value: 88auto foo12 Foo(Beijing); // Created Foo, type id: char const * __ptr64, value: Beijing// specifying the type of the templateFooint foo21(88); // Created Foo, type id: int, value: 88Fooconst char* foo22(Beijing); // Created Foo, type id: char const * __ptr64, value: Beijing// 2.User-defined deduction guides// not specifying the type of the templateauto foo31 Foo2(88); // Created Foo2, type id: int, value: 88auto foo32 Foo2(Beijing); // Created Foo2, type id: char const * __ptr64, value: Beijing// 3.the deduction for alias templatesMyAliasint alias1{ 88 };MyAliasstd::string alias2{ Beijing };std::cout alias1: alias1 \n; // alias1: 88std::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 5return 0;
} 执行结果如下图所示 GitHubhttps://github.com/fengbingchun/Messy_Test