本文介绍了C ++“概念"和"C ++"之间的关系是什么?和鸭打字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于模板和鸭子输入之间的关系,有一个更早的问题(8年前!):我借用并修改了有关C ++新功能的问题的标语线.

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++.

在C ++ 20中,将出现概念"的新功能,该功能看起来更像是鸭式打字功能.新的C ++概念"等同于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. 如果我们接受模板已经在编译时输入鸭子

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

  • 我不同意链接问题和Wikipedia页面上的已接受答案,因为在编译时而不是运行时执行同一件事并不是说服力的理由完全不同的名称.例如,请参见静态多态性与动态多态性.

  • 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.

如果我们(错误地)同意所链接问题的已接受答案以及维基百科,则

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

概念也在编译时发生

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.

这篇关于C ++“概念"和"C ++"之间的关系是什么?和鸭打字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 06:45