c++ - Why doesn't g++ pay attention to __attribute__((pure)) for virtual functions? -


According to

, __feature__ ((pure)) the compiler shows that the function has no side-effects, and therefore it The general subexpress may be subject to emissions.

This feature works for non-virtual functions, but not for virtual functions. For example, consider the following code:

  extract zero F (int); Square c {public: int a1 (); Int a2 () __trate __ ((pure)); Virtual Int B1 (); Virtual int b2 () __trate __ ((net)); }; Zero test_a1 (C * C) {if (C-> A1 ()) {F (C-> A1 ()); }} Zero test_a2 (C * C) {if (C-> A2 ()) {F (C-> A2 ()); }} Zero test_b1 (c * c) {if (c-> gt; b1 ()) {f (c-> gt; b1 ()); }} Zero test_b2 (c * c) {if (c-> gt; b2 ()) {f (c-> gt; b2 ()); }}  

When compiled with optimization enabled (either-O2 or-os), test_a2 () call only C :: a2 () one time, but test_b2 () call b2 () twice.

Is there any reason for this? Is that so, even if the implementation in the class C is pure, G ++ can not assume that implementation in every subclass is also pure? If so, is there any way to tell G ++ that this virtual function and the implementation of each subclass will be pure?

Without looking at the intersections of G ++, I suspect because G ++ can not believe it That the implementation of every subclass will be pure (as you said).

Can you convert b2 into a non-virtual pure wrap around a virtual non-pure method?


Comments