纯虚函数可能没有内联定义。为什么? [英] Pure virtual functions may not have an inline definition. Why?

查看:209
本文介绍了纯虚函数可能没有内联定义。为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

纯虚函数是那些虚拟的且具有纯指定符 = 0;

$ b的成员函数C ++ 03的
$ b

第10.4条第2款 告诉我们一个抽象类是什么,作为旁注: / p>

[注意:函数声明不能​​同时提供纯解释器和定义
-end note] p>

  struct C {
virtual void f()= 0 {}; // ill-formed
};

- 结束示例]



对于不太熟悉此问题的用户,请注意 纯虚拟函数可以有定义 ,但上述条款禁止此类定义出现inline(词法在同类中)。 (有关定义纯虚拟函数的用法,请参见此GotW



现在对于所有其他类型和类型的函数,它允许提供一个类的定义,这个限制似乎乍一看绝对人工和莫名其妙。来想想它,它似乎这样的第二次和随后的扫视:)但我相信,如果没有具体的原因,那里的限制将不存在。



我的问题是:有人 知道 这些具体原因吗?


$ b

  • MSVC允许PVF具有内联定义。所以不要惊讶:)

  • 这个问题中的单词 inline 并不指向 >关键字。


解决方案

SO线程为什么纯虚拟函数由0初始化? Jerry Coffin从Bjarne Stroustrup的 The Design& C ++的进化 ,第§13.2.3节,其中我认为是相关的部分:


好奇的 = 0 语法被选择超过
明显的替代引入
a新关键字纯或抽象,因为
我看到没有机会获得
a新关键字的时间。如果我
建议纯,版本2.0将有
没有抽象类运输。
给出一个更好的语法
和抽象类之间的选择,我选择抽象
类。与使用传统的C和C ++
约定,使用0来表示
不存在,而不是冒险延迟和
承担某些争论超过
纯。 = 0 语法符合
我认为函数体是函数的
初始化函数
b $ b(简单但通常足够)
虚拟函数集合的视图
被实现为
函数指针的向量。 [… ]


因此,当选择语法时,Bjarne将一个函数体视为声明器的一部分, c $ c> = 0 作为初始值的替代形式,表示无体 (或用他的话说,不在那里)。



这是因为一个人不能同时表示不在那里并有一个身体–



或者,仍然在这个概念图片中,有两个初始化器。



就我的心灵感应力,google-foo和软推理去。我推测,没有人被感兴趣的足够和贸易;向委员会提出一项建议,即取消这种纯粹的语法限制,并跟进所需的所有工作。因此,它仍然是这样。


Pure virtual functions are those member functions that are virtual and have the pure-specifier ( = 0; )

Clause 10.4 paragraph 2 of C++03 tells us what an abstract class is and, as a side note, the following:

[Note: a function declaration cannot provide both a pure-specifier and a definition —end note] [Example:

struct C {
virtual void f() = 0 { }; // ill-formed
};

—end example]

For those who are not very familiar with the issue, please note that pure virtual functions can have definitions but the above-mentioned clause forbids such definitions to appear inline (lexically in-class). (For uses of defining pure virtual functions you may see, for example, this GotW)

Now for all other kinds and types of functions it is allowed to provide an in-class definition, and this restriction seems at first glance absolutely artificial and inexplicable. Come to think of it, it seems such on second and subsequent glances :) But I believe the restriction wouldn't be there if there weren't a specific reason for that.

My question is: does anybody know those specific reasons? Good guesses are also welcome.

Notes:

  • MSVC does allow PVF's to have inline definitions. So don't get surprised :)
  • the word inline in this question does not refer to the inline keyword. It is supposed to mean lexically in-class

解决方案

In the SO thread “Why pure virtual function is initialized by 0?” Jerry Coffin provided this quote from Bjarne Stroustrup’s The Design & Evolution of C++, section §13.2.3, where I've added some emphasis of the part I think is relevant:

The curious =0 syntax was chosen over the obvious alternative of introducing a new keyword pure or abstract because at the time I saw no chance of getting a new keyword accepted. Had I suggested pure, Release 2.0 would have shipped without abstract classes. Given a choice between a nicer syntax and abstract classes, I chose abstract classes. Rather than risking delay and incurring the certain fights over pure, I used the tradition C and C++ convention of using 0 to represent "not there." The =0 syntax fits with my view that a function body is the initializer for a function and also with the (simplistic, but usually adequate) view of the set of virtual functions being implemented as a vector of function pointers. [ … ]

So, when choosing the syntax Bjarne was thinking of a function body as a kind of initializer part of the declarator, and =0 as an alternate form of initializer, one that indicated “no body” (or in his words, “not there”).

It stands to reason that one cannot both indicate “not there” and have a body – in that conceptual picture.

Or, still in that conceptual picture, having two initializers.

Now, that's as far as my telepathic powers, google-foo and soft-reasoning goes. I surmise that nobody's been Interested Enough™ to formulate a proposal to the committee about having this purely syntactical restriction lifted, and following up with all the work that that entails. Thus it's still that way.

这篇关于纯虚函数可能没有内联定义。为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆