خرید بک لینک

Vote count: 0

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() {
    retu 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?

asked 48 secs ago

برچسب: نویسنده: استخدام کار تاريخ: پنجشنبه 26 فروردين 1395 ساعت: 3:34

صفحه بندی