假设的、以前的-C++0x 概念问题

发布于 2024-07-30 04:56:21 字数 1509 浏览 6 评论 0原文

序言:我是 C++0x 游戏的较晚追随者,最近关于从 C++0x 标准中删除概念的争议促使我更多地了解它们。虽然我理解我所有的问题都是完全假设的——只要概念在未来一段时间内不会成为有效的 C++ 代码(如果有的话)——我仍然有兴趣了解更多有关概念的信息,特别是考虑到它将如何帮助我理解更多内容 在阅读了 C++0x(直到最近

)提出的一些概念介绍性材料后,我很难理清一些语法问题。 言归正传,我的问题是:

1)支持特定派生概念的类型(通过 auto 关键字隐式,或通过 Concept_maps 显式)是否也需要独立支持基本概念? 换句话说,从另一个概念派生出一个概念的行为(例如概念 B: A)是否隐式包含“不可见”requires 语句(在 B 内,requires A;)? 维基百科关于概念的页面指出了这一混乱:

就像在类继承中一样,类型 满足导出的要求 概念也符合要求 基本概念。

这似乎是说,类型只需要满足派生概念的要求,而不一定满足基本概念的要求,这对我来说没有意义。 我知道维基百科远非权威来源; 上面的描述是不是措辞不当?

2)列出类型名称的概念可以是“自动”吗? 如果是这样,编译器将如何自动映射这些类型名? 如果不是,是否还有其他情况下在概念上使用“auto”是无效的?

为了澄清这一点,请考虑以下假设代码:

template<typename Type>
class Dummy {};

class Dummy2 { public: typedef int Type; };

auto concept SomeType<typename T>
{
     typename Type;
}

template<typename T> requires SomeType<T>
void function(T t)
{}

int main()
{
    function(Dummy<int>()); //would this match SomeType?
    function(Dummy2()); //how about this?
    return 0;
}

这些类中的任何一个都会与 SomeType 匹配吗? 或者,对于涉及类型名的概念来说,concept_map 是必需的吗?

3)最后,我很难理解允许定义什么公理。 例如,我可以用一个概念定义一个逻辑上不一致的公理,例如

concept SomeConcept<typename T>
{
    T operator*(T&, int);

    axiom Inconsistency(T a)
    {
         a * 1 == a * 2;
    }
} 

那会做什么? 这还有效吗?

我知道这是一个很长的问题,所以我提前感谢您。

(Preamble: I am a late follower to the C++0x game and the recent controversy regarding the removal of concepts from the C++0x standard has motivated me to learn more about them. While I understand that all of my questions are completely hypothetical -- insofar as concepts won't be valid C++ code for some time to come, if at all -- I am still interested in learning more about concepts, especially given how it would help me understand more fully the merits behind the recent decision and the controversy that has followed)

After having read some introductory material on concepts as C++0x (until recently) proposed them, I am having trouble wrapping my mind around some syntactical issues. Without further ado, here are my questions:

1) Would a type that supports a particular derived concept (either implicitly, via the auto keyword, or explicitly via concept_maps) also need to support the base concept indepdendently? In other words, does the act of deriving a concept from another (e.g. concept B<typename T> : A<T>) implicitly include an 'invisible' requires statement (within B, requires A<T>;)? The confusion arises from the Wikipedia page on concepts which states:

Like in class inheritance, types that
meet the requirements of the derived
concept also meet the requirements of
the base concept.

That seems to say that a type only needs to satisfy the derived concept's requirements and not necessarily the base concept's requirements, which makes no sense to me. I understand that Wikipedia is far from a definitive source; is the above description just a poor choice of words?

2) Can a concept which lists typenames be 'auto'? If so, how would the compiler map these typenames automatically? If not, are there any other occasions where it would be invalid to use 'auto' on a concept?

To clarify, consider the following hypothetical code:

template<typename Type>
class Dummy {};

class Dummy2 { public: typedef int Type; };

auto concept SomeType<typename T>
{
     typename Type;
}

template<typename T> requires SomeType<T>
void function(T t)
{}

int main()
{
    function(Dummy<int>()); //would this match SomeType?
    function(Dummy2()); //how about this?
    return 0;
}

Would either of those classes match SomeType? Or is a concept_map necessary for concepts involving typenames?

3) Finally, I'm having a hard time understanding what axioms would be allowed to define. For example, could I have a concept define an axiom which is logically inconsistent, such as

concept SomeConcept<typename T>
{
    T operator*(T&, int);

    axiom Inconsistency(T a)
    {
         a * 1 == a * 2;
    }
} 

What would that do? Is that even valid?

I appreciate that this is a very long set of questions and so I thank you in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

江心雾 2024-08-06 04:56:21

我使用了最新的 C++0x 草案 N2914(其中仍然有概念措辞)作为以下答案的参考。

1)概念就像接口一样。 如果您的类型支持一个概念,它也应该支持所有“基本”概念。 从类型客户端的角度来看,您引用的维基百科声明是有意义的 - 如果他知道 T 满足概念 Derived,那么他也知道它满足概念基础。 从类型作者的角度来看,这自然意味着两者都必须实现。 参见 14.10.3/2。

2) 是的,具有typename成员的概念可以是auto。 如果这些成员用于同一概念中的函数成员的定义,则可以自动推导它们。 例如,迭代器的value_type可以被推导为其operator*的返回类型。 但是,如果类型成员未在任何地方使用,则不会推导它,因此不会隐式定义。 在您的示例中,无法将 SomeType::Type 推导出为 DummyDummy1,如 Type 不被该概念的其他成员使用,因此两个类都不会映射到该概念(事实上,没有一个类可以自动映射到它)。 参见 14.10.1.2/11 和 14.10.2.2/4。

3)公理是规范的一个弱点,并且它们不断更新以使其具有一些(更多)意义。 就在从草案中提取概念之前,有一个 论文发生了很大的变化 - 阅读它并看看它是否对您更有意义,或者您仍然对此有疑问。

对于您的具体示例(考虑语法差异),这意味着编译器将被允许将表达式 (a*1) 视为与 (a*2),出于语言的“as-if”规则的目的(即编译器允许进行任何它想要的优化,只要结果表现得就好像没有)。 然而,编译器不需要以任何方式验证公理的正确性(因此它们被称为公理!) - 它只是按照它们本来的样子来验证它们。

I've used the most recent C++0x draft, N2914 (which still has concepts wording in it) as a reference for the following answer.

1) Concepts are like interfaces in that. If your type supports a concept, it should also support all "base" concepts. Wikipedia statement you quote makes sense from the point of view of a type's client - if he knows that T satisfies concept Derived<T>, then he also knows that it satisfies concept Base<T>. From type author perspective, this naturally means that both have to be implemented. See 14.10.3/2.

2) Yes, a concept with typename members can be auto. Such members can be automatically deduced if they are used in definitions of function members in the same concept. For example, value_type for iterator can be deduced as a return type of its operator*. However, if a type member is not used anywhere, it will not be deduced, and thus will not be implicitly defined. In your example, there's no way to deduce SomeType<T>::Type for either Dummy or Dummy1, as Type isn't used by other members of the concept, so neither class will map to the concept (and, in fact, no class could possibly auto-map to it). See 14.10.1.2/11 and 14.10.2.2/4.

3) Axioms were a weak point of the spec, and they were being constantly updated to make some (more) sense. Just before concepts were pulled from the draft, there was a paper that changed quite a bit - read it and see if it makes more sense to you, or you still have questions regarding it.

For your specific example (accounting for syntactic difference), it would mean that compiler would be permitted to consider expression (a*1) to be the same as (a*2), for the purpose of the "as-if" rule of the language (i.e. the compiler permitted to do any optimizations it wants, so long as the result behaves as if there were none). However, the compiler is not in any way required to validate the correctness of axioms (hence why they're called axioms!) - it just takes them for what they are.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文