What's the relationship between C++ “concept” and duck typing?

风流意气都作罢 提交于 2021-02-18 22:10:28

问题


There was an earlier question (8 years ago!) about the relationship between templates and duck typing here: What's the relationship between C++ template and duck typing? I've borrowed and modified the tag line for my question on a new feature of C++.

With C++20 there will be the new feature of "concept" that looks much more like a duck-typing feature. Is it correct that the new C++ "concept" is equivalent to duck typing for C++? If not, how is it different?


回答1:


With C++20 there will be the new feature of "concept" that looks much more like a duck-typing feature

Not really.

  1. If we accept that templates are already compile-time duck typing

    • I disagree with both the accepted answer on the linked question and the Wikipedia page, because doing the same thing at compile time instead of run time is not a persuasive reason to give it a wholly different name. See static vs. dynamic polymorphism for example.

    • then concepts are not obviously more duck-type-y than templates already are

    • the other argument here is that duck typing generally supposes you express your type constraints by attempting to use the type. Concepts are further from this model than templates already are.

  2. If we agree (wrongly) with both the accepted answer on the linked question, and with Wikipedia, then

    • the reason templates are not (an example of) duck typing is that they happen at compile time

    • concepts also happen at compile time

    • QED

Concepts take what templates already do (which may or may not be, but definitely is, duck typing) and give finer-grained options for specialization, and/or more clearly express type constraints, and (hopefully) help to generate better errors.

The quality of duckiness is invariant under all these operations.




回答2:


It really all depends on how you define "duck typing".

If you use the dictionary definition ("If it walks like a duck and it quacks like a duck, then it must be a duck"), then pre-concepts templates are a form of duck typing.

The argument that templates are not duck typing is essentially an argument from the way the term is commonly used: languages that are said to use "duck typing" (Python, Lua, etc) do most/all of their verification that the duck is in fact a duck at runtime. Templates do their equivalent operations at compile time and therefore they do not represent duck typing.

Note that concepts doesn't affect either of these interpretations. If you consider runtime checking to be a fundamental part of the nature of "duck typing", then templates aren't duck typing even with concepts.

So if you felt that templates are duck typing pre-concepts, then you likely feel the same way post-concepts. And vice-versa.



来源:https://stackoverflow.com/questions/58737419/whats-the-relationship-between-c-concept-and-duck-typing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!