2

Consider the following template class

template<typename T>
struct Caller {
    void func(const T &t) { t.func(); }
    void gunc(const T &t) { t.gunc(); }
};

Now let some class Target only provide the member function func() but not gunc(), i.e.

struct Target {
    void func() const { /* ... /* }
};

is the template instantiation Caller<Target> valid?

GCC, clang as well as VC++ accept such template instantiations. Of course, calling Caller<Target>::gunc() leads to an error but Caller<Target>::func() works just fine and as intended.

Now the question: What is the background for this permissive behavior and where are the relevant paragraphs in C++ standard.

bogdan
  • 9,229
  • 2
  • 33
  • 48
phlipsy
  • 2,899
  • 1
  • 21
  • 37

1 Answers1

4

It's specified in the standard, under Templates (14), Template instantiation and specialization (14.7), Implicit instantiation (14.7.1).

3 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.

And

11 An implementation shall not implicitly instantiate a function template, a member template, a non-virtual member function, a member class, or a static data member of a class template that does not require instantiation.

Dutow
  • 5,638
  • 1
  • 30
  • 40
  • The first paragraph only refers to function templates, doesn't it? On the other hand, the second paragraph seems to explain the behavior. – phlipsy Jul 22 '16 at 11:07
  • No, they are function templates. It is in 14.5.1.1 - Member functions of class templates. But otherwise the second is more important for your question, as it explain why the compiler isn't allowed to generate an error message. The first would just imply that it's not required to instantiate it, but wouldn't disallow it. – Dutow Jul 22 '16 at 11:28