3

In C++, suppose I write a template function

template<typename T>
void func(T obj){
    obj.print();
}

But for some class, there might be not print() member function defined, which causes compilation errors. However, obj.print() may be not so important. Instead of add print() to the class, I prefer to make print() not called.

This may be a weird question. But just wondering if anyone ever saw such question.

M.M
  • 138,810
  • 21
  • 208
  • 365
Tim
  • 355
  • 1
  • 8
  • "wired"? Why is everyone confusing that with "weird"? They are not *that* similar... – The Paramagnetic Croissant May 28 '14 at 22:33
  • Doesn't seem to make much sense. If you call it it has to exist. How could the compiler know that print is not important for you? For all it knows, that function call might be doing something crucial for `func` to work. You can't just simply omit parts of function definitions. – isarandi May 28 '14 at 22:36
  • @user: No, but they're close enough and are both valid words, meaning spell-check doesn't pick it up. It's the first time *I* see that particular typo if it makes you feel better :-) – Cameron May 28 '14 at 22:37
  • 2
    See also http://stackoverflow.com/a/13787502/21475 – Cameron May 28 '14 at 22:42

0 Answers0