为什么标准不允许“virtual void funcFoo() = 0 { }”?
[我无法找到正确的答案。如果这个问题已经得到解答,请给我指出正确的链接。]
我知道这样做是非法的,
class Base
{
public:
virtual void funcFoo() = 0 {} //illegal. should be defined outside the class body
virtual ~Base() {}
};
但这在 VS2008 上工作得很好。 我想知道为什么标准不允许这样做?
在android上,我发现我必须像这样定义函数内联,
inline void Base::funcFoo() {}
而不仅仅是,
void Base::funcFoo() {}
这里隐式内联和显式内联有什么区别?编译器做了什么不同的事情?
[I couldn't find a proper answer to this. Kindly point me to proper links if this is already answered.]
I know that it illegal to do something like this,
class Base
{
public:
virtual void funcFoo() = 0 {} //illegal. should be defined outside the class body
virtual ~Base() {}
};
But this works fine on VS2008.
I want to know why this is disallowed by the standard?
On android, I see that I have to define the function inline like this,
inline void Base::funcFoo() {}
instead of just,
void Base::funcFoo() {}
what is the diference in implicit inlining and explicit inlining here? what is the compiler doing different?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根据第 §10.4/2 节(在注释中),这是不正确的,
希望它能回答您的问题。
现在请参阅@John Dibling 发表的第一条评论(如下),不幸的是,您的“为什么”问题的答案不在标准中,如果
“格式不正确”
不在标准中没有给你一个可以接受的答案。语言语法根本不允许这样做。:-)That is ill-formed according to section §10.4/2 which says (in the note) that,
Hope it answers your question.
Now please refer to the first comment (below) made by @John Dibling, as unfortunately the answer to your "why" question isn't in the Standard, IF
"that is ill-formed"
isn't an acceptable answer to you. The language grammar simply doesn't allow it.:-)我认为这个问题没有太多答案。它以前出现过一次(可能是在 Usenet 上而不是 SO 上——我不记得了),所以我做了一些查找。我实际上并没有想出太多东西。据我所知,这就是 Bjarne 最初设计的方式。虽然它可以改变,但我找不到任何向委员会提出改变的建议,也没有任何迹象表明委员会已经辩论、讨论或考虑过它。我的猜测是,它被认为是“足够好”,所以没有人愿意付出太多(任何?)努力来改变它。
I don't think there is much of an answer to this. It came up once before (possibly on Usenet instead of SO -- I don't remember), so I did some looking. I didn't really come up with much of anything. As far as I can tell, that's how Bjarne originally devised it. Although it could be changed, I couldn't find any proposals to the committee that it be changed, nor any indication that the committee has even debated, discussed, or considered it at all. My guess would be that it's considered "good enough" the way it is, so nobody's willing to put in much (any?) effort to change it.
第一个问题已经得到解答——标准只是不允许这样做。
第二个问题是:
不同之处在于,第一个变体可以放置在头文件中,该头文件可以由多个源文件包含。第二个变体必须恰好放置在一个源文件中。
The first question is already answered -- the standard simply disallows it.
The second question is:
The difference is that the first variant can be placed in a header file, which can be included by more than one source file. The second variant must be place in exactly one source file.