When I compile the following with g++ --std=c++98 -Wall -Werror -Wpedantic Test.cc , there is 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 the compiler to complain that ExampleClass does not implement the bar method.
But it looks like he is only complaining if I really use the foo method.
Can I rely on this behavior on any compilers compatible with C ++ 98 and C ++ 11?
My understanding of patterns previously was that whenever a pattern is instantiated, all of it is copied with T replaced by the pattern argument. Isn't that how templates work?
source share