为什么标准不允许“virtual void funcFoo() = 0 { }”?

发布于 2024-11-02 11:57:46 字数 508 浏览 0 评论 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 技术交流群。

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

发布评论

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

评论(3

乜一 2024-11-09 11:57:46

根据第 §10.4/2 节(在注释中),这是不正确的,

函数声明不能​​同时提供纯说明符和定义

[Example:

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

—end example]

希望它能回答您的问题。

现在请参阅@John Dibling 发表的第一条评论(如下),不幸的是,您的“为什么”问题的答案不在标准中,如果 “格式不正确” 不在标准中没有给你一个可以接受的答案。语言语法根本不允许这样做。:-)

That is ill-formed according to section §10.4/2 which says (in the note) that,

a function declaration cannot provide both a pure-specifier and a definition

[Example:

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

—end example]

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.:-)

唠甜嗑 2024-11-09 11:57:46

我认为这个问题没有太多答案。它以前出现过一次(可能是在 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.

半步萧音过轻尘 2024-11-09 11:57:46

第一个问题已经得到解答——标准只是不允许这样做。

第二个问题是:

在 Android 上,我发现我必须
像这样定义内联函数,

inline void Base::funcFoo() {}

不仅仅是,

void Base::funcFoo() {}

什么是
隐式内联和隐式内联的区别
这里显式内联?什么是
编译器做了不同的事情?

不同之处在于,第一个变体可以放置在头文件中,该头文件可以由多个源文件包含。第二个变体必须恰好放置在一个源文件中。

The first question is already answered -- the standard simply disallows it.

The second question is:

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?

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.

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