Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 850785
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T07:19:19+00:00 2026-05-15T07:19:19+00:00

According to the GCC documentation , __attribute__((pure)) tells the compiler that a function has

  • 0

According to the GCC documentation, __attribute__((pure)) tells the compiler that a function has no side-effects, and so it can be subject to common subexpression elimination.

This attribute appears to work for non-virtual functions, but not for virtual functions. For example, consider the following code:

extern void f( int );

class C {
 public:
   int a1();
   int a2() __attribute__((pure));
   virtual int b1();
   virtual int b2() __attribute__((pure));
};

void test_a1( C *c ) {
   if( c->a1() ) {
      f( c->a1() );
   }
}

void test_a2( C *c ) {
   if( c->a2() ) {
      f( c->a2() );
   }
}

void test_b1( C *c ) {
   if( c->b1() ) {
      f( c->b1() );
   }
}

void test_b2( C *c ) {
   if( c->b2() ) {
      f( c->b2() );
   }
}

When compiled with optimization enabled (either -O2 or -Os), test_a2() only calls C::a2() once, but test_b2() calls b2() twice.

Is there a reason for this? Is it because, even though the implementation in class C is pure, g++ can’t assume that the implementation in every subclass will also be pure? If so, is there a way to tell g++ that this virtual function and every subclass’s implementation will be pure?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T07:19:20+00:00Added an answer on May 15, 2026 at 7:19 am

    Without looking into g++’s internals, I suspect it’s because g++ can’t assume that every subclass’s implementation will be pure (like you said).

    Can you turn b2 into a non-virtual pure wrapper around a virtual non-pure method?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Apparently (at least according to gcc -std=c99 ) C99 doesn't support function overloading. The
According to the gcc docs , memcmp is not an intrinsic function of GCC.
According to Hibernate documentation : After observing that arrays cannot be lazy , you
According to the documentation a mutex can be initialized in two ways: Using the
Recently, I noticed some people mentioning that std::list::size() has a linear complexity. According to
According to my compiler gcc-4.6 the call to func in the example below is
According MSDN, FxCop is an application that analyzes managed code assemblies (code that targets
Actually I have read about that I can write Objective-C app on Linux (using
According to posts from 2008 (I can't find it right now), glibc heap check
Is there a way for GCC to produce a warning while linking libraries that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.