How can a pure virtual function be invoked without its name being explicitly qualified?

138 views
Skip to first unread message

João Afonso

unread,
Mar 4, 2017, 4:40:32 PM3/4/17
to ISO C++ Standard - Discussion
[basic.def.odr]/3 contains the following remarks:

"A virtual member function is odr-used if it is not pure. A function whose name appears as a potentially-evaluated expression is odr-used if it is
the unique lookup result or the selected member of a set of overloaded functions (3.4, 13.3, 13.4), unless it is a pure virtual function and either
its name is not explicitly qualified or the expression forms a pointer to member (5.3.1)."

There seems to be an error in the highlighted text above, as I can't imagine a pure virtual function being invoked, in a situation where its name is not explicitly qualified.
Consider the example given in this answer in Stackoverflow, which shows a pure virtual function being invoked with a qualified name. 

Nicol Bolas

unread,
Mar 4, 2017, 7:21:10 PM3/4/17
to ISO C++ Standard - Discussion

As explained in the answer to the Stack Overflow question you asked, if you have a pointer to `Foo`, and it has a pure virtual function `bar`, and you do `ptr->bar()`, then that expression under overload resolution selects a pure virtual function. The fact that at runtime this will not actually call that function is irrelevant. That section doesn't talk about what gets invoked; it talks about what is ODR used. And the statement you emphasized is simply spelling out that pure virtual functions aren't ODR used just because you tried to call them.

João Afonso

unread,
Mar 5, 2017, 5:00:18 AM3/5/17
to ISO C++ Standard - Discussion

On Saturday, March 4, 2017 at 9:21:10 PM UTC-3, Nicol Bolas wrote:

As explained in the answer to the Stack Overflow question you asked, if you have a pointer to `Foo`, and it has a pure virtual function `bar`, and you do `ptr->bar()`, then that expression under overload resolution selects a pure virtual function. The fact that at runtime this will not actually call that function is irrelevant. That section doesn't talk about what gets invoked; it talks about what is ODR used. And the statement you emphasized is simply spelling out that pure virtual functions aren't ODR used just because you tried to call them.

Notice that I emphasized some words above in your answer. That answered my question. Thanks.

Ayrosa

unread,
Jan 8, 2018, 12:18:17 PM1/8/18
to ISO C++ Standard - Discussion


On Saturday, March 4, 2017 at 9:21:10 PM UTC-3, Nicol Bolas wrote:

As explained in the answer to the Stack Overflow question you asked, if you have a pointer to `Foo`, and it has a pure virtual function `bar`, and you do `ptr->bar()`, then that expression under overload resolution selects a pure virtual function. The fact that at runtime this will not actually call that function is irrelevant. That section doesn't talk about what gets invoked; it talks about what is ODR used. And the statement you emphasized is simply spelling out that pure virtual functions aren't ODR used just because you tried to call them.

But why is the function bar() selected by overload resolution in this case? I reproduce below the example given by Brian in his answer .

struct Foo {
    virtual void bar() = 0;
};

void quux(Foo* f) {
    f->bar();
}
Reply all
Reply to author
Forward
0 new messages