What's the relationship between C++ template and duck typing?

后端 未结 7 2032
一向
一向 2020-12-29 17:48

To me, C++ template used the idea of duck typing, is this right? Does it mean all generic types referenced in template class or method are duck type?

7条回答
  •  无人及你
    2020-12-29 18:29

    To me C++ templates are a compile-time version of duck typing. The compiler will compile e.g. Class and as long as your Duck has all needed types it will instantiate a class.

    If something is not correct(e.g. copy constructor missing) the compilation fails. The counterpart in real ducktyping is a failure when you call a function with a non-duck type. And here it would occur at runtime.

提交回复
热议问题