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

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.


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.