Wednesday, 15 February 2012

c++ - Member function instantiation -



c++ - Member function instantiation -

the next 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() { homecoming t(); } }; int main(int argc, char** argv) { dummy<non_default_constructible> d; homecoming 0; }

the tricky part dummy<non_default_constructible>::new_t() ill-formed, not prevent compiler instantiating dummy<non_default_constructible>.

is behaviour specified standard? , relevant sections/keywords?

the fellow member functions of class template instantiated when required context, means not see error until seek utilize new_t(). related section c++ standard is:

§ 14.7.1 implicit instantiation [temp.inst]

unless function template specialization has been explicitly instantiated or explicitly specialized, function template specialization implicitly instantiated when specialization referenced in context requires function definition exist. unless phone call function template explicit specialization or fellow member function of explicitly specialized class template, default argument function template or fellow member function of class template implicitly instantiated when function called in context requires value of default argument.

[ 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, , // instantiation of z<char>::g() required }

nothing in illustration requires class z<double>, z<int>::g(), or z<char>::f() implicitly instantiated. — end example ]

c++ templates

No comments:

Post a Comment