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

问题描述

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

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

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

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

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

- 结束示例]



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



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



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


$ b

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

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


解决方案

SO线程 Jerry Coffin从Bjarne Stroustrup的,第§13.2.3节,其中我认为是相关的部分:

因此,当选择语法时,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:

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.

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

08-22 19:21