c++ - What is dominance in the context of virtual functions? -
c++ - What is dominance in the context of virtual functions? -
code sample:
consider next diamond hierarchy:
struct { virtual void f(){} void g(){} }; struct b : virtual { virtual void f() override{} void g(){} }; struct c : virtual { }; struct d: b, c { }; int main() { d d; d.f(); //b::f called d.g(); //b::g called }
what understand: as far non-virtual function g
concerned, clear: name b::g
hides a::g
, though name a::g
reached without beingness hidden through c
. there no ambiguity. b::g
called. the standard explicitly confirms in 10.2 p.10:
[ note: when virtual base of operations classes used, hidden declaration can reached along path through subobject lattice not pass through hiding declaration. not ambiguity. identical utilize non-virtual base of operations classes ambiguity; in case there no unique instance of name hides others. — end note ] [example:
additionally, this answer related question provided exhaustive explanation of issue.
the problem:what don't understand how quote mentioned above pertains virtual function f
. there no name hiding involved f
, there? overriding involved, , 10.3 p.2 reads:
a virtual fellow member function c::vf of class object s final overrider unless derived class (1.8) of s base of operations class subobject (if any) declares or inherits fellow member function overrides vf. in derived class, if virtual fellow member function of base of operations class subobject has more 1 final overrider programme ill-formed.
now, seems me, virtual function f
fits definition of not having final overrider , programme should ill-formed. it's not. or it? msvc compiles fine, next warning:
warning c4250: 'd' : inherits 'b::b::f' via dominance
to honest, had never come across term "dominance" before. when search in standard, has single occurrence, in index, , refers me chapter first quote comes from. and, mentioned, quote seems pertain name hiding rather virtual function overriding.
questions: doesf
have more 1 final overrider in d? does rule of dominance apply in case? how follow standard?
i'll quote [10.3]/2
again:
a virtual fellow member function c::vf
of class object s
final overrider unless derived class (1.8) of s
base of operations class subobject (if any) declares or inherits fellow member function overrides vf
. in derived class, if virtual fellow member function of base of operations class subobject has more 1 final overrider programme ill-formed.
so in illustration a::f
not final overrider, because d
(the derived class) inherits b::f
overrides it. b::f
final overrider, , called.
the programme ill-formed if there more 1 final overrider (for illustration if c
overrode f
), there one, fine.
clang , gcc compile without single warning.
answering question, the documentation on c4250 warning describes situation, , dominance apparently means behavior described in [10.3]/2
.
c++ c++11 language-lawyer virtual-functions virtual-inheritance
Comments
Post a Comment