Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Template class method does not cause error -- is this part of the standard?

When I compile the following with g++ --std=c++98 -Wall -Werror -Wpedantic Test.cc, there's no error.

template <class T>
struct TemplateClass {
  T *ptr;

  TemplateClass(T *p): ptr(p) {}

  int foo() {
    return ptr->bar();
  }
};

struct ExampleClass {
};

int main() {
  TemplateClass<ExampleClass> x(new ExampleClass());
}

I expected that the compiler would complain that ExampleClass doesn't implement method bar.

But it looks like it only complains if I actually use the method foo.

Can I rely on this behavior on any C++98 and C++11 compliant compilers?

My understanding of templates before was that whenever a template is instantiated, the entirety of the body is copied with T replaced with the template argument. Is this not how templates work?

like image 275
math4tots Avatar asked Apr 13 '16 20:04

math4tots


People also ask

Which statement is correct about template class?

Which of the following is correct about templates? Explanation: Templates are used for generic programming hence allowing to write a single function for all data types. It is a type of compile time polymorphism.

How the template class is different from the normal class?

How the template class is different from the normal class? Explanation: Size of the object of template class varies depending on the type of template parameter passed to the class. Due to which each object occupies different memories on system hence saving extra memories.

Can default argument be used with the template class?

Can default arguments be used with the template class? Explanation: The template class can use default arguments.

What is a template class what is the need for template class?

Definition. As per the standard definition, a template class in C++ is a class that allows the programmer to operate with generic data types. This allows the class to be used on many different data types as per the requirements without the need of being re-written for each type.


1 Answers

This is the correct behaviour according to the standard. The definition of foo is not instantiated until used in a context that requires it to exist. Emphasis mine in the below:

C++03, [temp.inst]/1:

The implicit instantiation of a class template specialization causes the implicit instantiation of the declarations, but not of the definitions or default arguments, of the class member functions, member classes, static data members and member templates; and it causes the implicit instantiation of the definitions of member anonymous unions. Unless a member of a class template or a member template has been explicitly instantiated or explicitly specialized, the specialization of the member is implicitly instantiated when the specialization is referenced in a context that requires the member definition to exist; ...

C++11, [temp.inst]/1 and [temp.inst]/2:

Unless a class template specialization has been explicitly instantiated (14.7.2) or explicitly specialized (14.7.3), the class template specialization is implicitly instantiated when the specialization is referenced in a context that requires a completely-defined object type or when the completeness of the class type affects the semantics of the program. The implicit instantiation of a class template specialization causes the implicit instantiation of the declarations, but not of the definitions or default arguments, of the class member functions, member classes, scoped member enumerations, static data members and member templates; ... Unless a member of a class template or a member template has been explicitly instantiated or explicitly specialized, the specialization of the member is implicitly instantiated when the specialization is referenced in a context that requires the member definition to exist; ...

like image 163
Brian Bi Avatar answered Nov 11 '22 07:11

Brian Bi