成员函数实例化 [英] Member function instantiation

查看:32
本文介绍了成员函数实例化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下在 GCC 4.8.1 上编译(使用 --std=c++11):

struct non_default_constructible { non_default_constructible() = delete;};模板结构虚拟{T new_t() { 返回 T();}};int main(int argc, char** argv) {虚拟d;返回0;}

棘手的部分是 dummy::new_t() 显然格式错误,但这并不能阻止编译器实例化 dummy.

这是标准规定的行为吗?什么是相关的部分/关键字?

解决方案

类模板的成员函数仅在上下文需要时才会实例化,这意味着您在尝试使用 new_t 之前不会看到任何错误().C++ 标准的相关部分是:

<块引用>

§ 14.7.1 隐式实例化 [temp.inst]

  1. 除非函数模板特化已被显式实例化或显式特化,函数模板特化在需要函数定义存在的上下文中被引用时隐式实例化.除非调用是对函数模板显式特化或显式特化类模板的成员函数,否则函数模板的默认参数或类模板的成员函数在需要默认参数的值.

  2. [ 示例:

    template结构 Z {无效 f();无效 g();};无效 h() {Z一种;//类 Z 的实例化必需的Z 字符 * p;//类 Z 的实例化不需要Z <double >* q;//类 Z 的实例化不需要a.f();//需要实例化 Z::f()p->g();//类 Z 的实例化需要,和//需要实例化 Z::g()}

    此示例中没有任何内容需要 class ZZ::g()Z::f() 是隐式的实例化.— 结束示例 ]

The following compiles on GCC 4.8.1 (with --std=c++11):

struct non_default_constructible { non_default_constructible() = delete; };

template<class T>
struct dummy {
    T new_t() { return T(); }
};

int main(int argc, char** argv) {
    dummy<non_default_constructible> d;
    return 0;
}

The tricky part is that dummy<non_default_constructible>::new_t() is obviously ill-formed, but that does not prevent the compiler from instantiating dummy<non_default_constructible>.

Is this the behaviour specified by the standard? And what would be the relevant sections/keywords?

解决方案

The member functions of a class template are instantiated only when required by a context, which means you will not see any error until you try to use new_t(). The related section from the C++ standard is:

§ 14.7.1 Implicit instantiation [temp.inst]

  1. Unless a function template specialization has been explicitly instantiated or explicitly specialized, the function template specialization is implicitly instantiated when the specialization is referenced in a context that requires a function definition to exist. Unless a call is to a function template explicit specialization or to a member function of an explicitly specialized class template, a default argument for a function template or a member function of a class template is implicitly instantiated when the function is called in a context that requires the value of the default argument.

  2. [ Example:

    template<class T> struct Z {
      void f();
      void g();
    };
    
    void h() {
      Z<int> a;     // instantiation of class Z<int> required
      Z<char>* p;   // instantiation of class Z<char> not required
      Z<double>* q; // instantiation of class Z<double> not required
      a.f();        // instantiation of Z<int>::f() required
      p->g();       // instantiation of class Z<char> required, and
                    // instantiation of Z<char>::g() required
    }
    

    Nothing in this example requires class Z<double>, Z<int>::g(), or Z<char>::f() to be implicitly instantiated. — end example ]

这篇关于成员函数实例化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆