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 8397349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:45:52+00:00 2026-06-09T20:45:52+00:00

The GCC C++ compiler offers a family of extensions via function attributes , such

  • 0

The GCC C++ compiler offers a family of extensions via function attributes, such as:

int square(int) __attribute__((const));

Two attributes in particular, const and pure, allow you to declare that a function’s evaluation has no side effects and depends only on its arguments (const), or only on its arguments and global variables (pure). This allows for common subexpression elimination, which may have the effect that such a function gets called fewer times than it is written in the code.

My question is whether this can be used safely, correctly and sensibly for virtual member functions:

struct Foo
{
    virtual int square(int) __attribute__((pure));   // does that make sense?
};

Does this have any sensible semantics? Is it allowed at all? Or is it just ignored? I’m afraid I can’t find the answer to this in the GCC documentation.

The reason for this question is that there is a family of compiler options -Wsuggest-attribute which make GCC produce suggestions of where those attributes might be placed to improve the code. However, it seems to end up making these suggestions even for virtual functions, and I wonder whether those suggestions should be taken seriously.

  • 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-06-09T20:45:54+00:00Added an answer on June 9, 2026 at 8:45 pm

    The first question is whether these attributes even have valid semantics for virtual methods. In my opinion they do. I would expect that if a virtual function was labelled pure you would be promising the compiler that all implementations only rely on their arguments and the data in global memory (and do not change that), where data in global memory would also include the contents of the object. If a virtual function were labelled const this would mean it could depend on only on its arguments, it would not even be allowed to inspect the contents of the object. The compiler would have to enforce that all overriding virtual methods declare attributes at least as strong as their parents.

    The next question is whether GCC uses these attributes to make optimisations. In the following test program you can see that version 4.6.3 does not (try compiling to assembler with -O3 and you will see the loop is unrolled).

    struct A {
        virtual int const_f(int x) __attribute__((const)) = 0;
    };
    
    int do_stuff(A *a) {
        int b = 0;
        for (int i=0; i<10; i++) {
            b += a->const_f(0);
        }
        return b;
    }
    

    Even in the following program where the type is known at compile time the compiler does not optimize the loop.

    struct A {
        virtual int const_f(int x) __attribute__((const)) = 0;
    };
    
    struct B : public A {
        int const_f(int x) __attribute__((const));
    };
    
    int do_stuff(B *b) {
        int c = 0;
        for (int i=0; i<10; i++) {
            c += b->const_f(0);
        }
        return c;
    }
    

    Removing the inheritance from A (and thus making the method non-virtual) allows the compiler to make the expected optimisation.

    There is no standard or documentation regarding these attributes, so the best reference we can have is the implementation. As they currently have no effect I would suggest avoiding their use on virtual methods in case the behaviour changes unexpectedly in the future.

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

Sidebar

Related Questions

Is it possible to prevent the gcc compiler from inlining a particular function. If
I was trying to execute this code through gcc compiler: #include <stdio.h> int main()
On compiling given program in GCC Compiler : int main() { int a=2,b=3; (a>1)?b=10:b=50;
I am using the GCC compiler but I want the to code the actually
I compiled this (gcc compiler) : #include< stdio.h> main() { unsigned char ch; FILE
I compiled this (gcc compiler): #include <stdio.h> main() { struct { a:1; b:2; }
I am working on a platform with a gcc compiler however boost cannot compile
Do you know how strong VLIW architectures support exists in GCC compiler infrastructure? I
I'm using the Code::Blocks IDE with the GNU GCC compiler. struct test { char
I was working on a program in Netbeans on Linux using a gcc compiler

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.