Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Member function instantiation

Tags:

c++

templates

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?

like image 910
gTcV Avatar asked Sep 30 '14 14:09

gTcV


1 Answers

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 ]

like image 92
Piotr Skotnicki Avatar answered Oct 26 '22 20:10

Piotr Skotnicki