Difference between instantiation and specialization in c++ templates

前端 未结 6 956
眼角桃花
眼角桃花 2020-11-28 23:40

What is the difference between specialization and instantiation in context of C++ templates. From what I have read so far the following is what I have understood about speci

6条回答
  •  情歌与酒
    2020-11-29 00:21

    A specialized template is no longer just a template. Instead, it is either an actual class or an actual function.

    A specialization is from either an instantiation or an explicit specialization, cf 14.7.4 below.

    An instantiation is based on a primary template definition. A sample implicit class template instantiation,

    template
    class foo {}
    
    foo foo_int_object;
    

    A sample explicit class template instantiation,

    template class foo;
    

    An explicit specialization has a different definition from it's primary template.

    template<>
    class foo {}
    

    // extract from standard

    14 Templates

    14.7 Template instantiation and specialization

    4 An instantiated template specialization can be either implicitly instantiated (14.7.1) for a given argument list or be explicitly instantiated (14.7.2). A specialization is a class, function, or class member that is either instantiated or explicitly specialized (14.7.3).

提交回复
热议问题