This is the mail archive of the mailing list for the GCC project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: void function declared attribute const

On 02/08/17 15:41 -0600, Martin Sebor wrote:
Hi Honza,

While testing improvements to GCC attribute handling I came
across the warning below:

In file included from /ssd/src/gcc/81544/libstdc++-v3/src/c++98/ /ssd/build/gcc-81544/x86_64-pc-linux-gnu/libstdc++-v3/include/ext/mt_allocator.h:359:43: warning: ‘const’ attribute on function returning ‘void’ [-Wattributes]
      _M_destroy_thread_key(void*) throw ();

Git log shows you added the attribute in r146330 and there was
a lively debate preceding the change on the libstdc++ mailing

A patch including this function was posted here:

I've re-read much of the discussion but couldn't find my answer
there so I'm wondering if you could help me understand the
rationale for it.  (Btw., I understand what the attribute does,
I'm just not sure why it's helpful or even used in this instance.

The function is declared like this in the header:

     // XXX GLIBCXX_ABI Deprecated
     _GLIBCXX_CONST void
     _M_destroy_thread_key(void*) throw ();

and defined like so in

 __pool<true>::_M_destroy_thread_key(void*) throw () { }

So the definition trivially meets the requirements on a const
function, and calls to it will be eliminated (with optimization).

My question is: since the function is deprecated and its
definition only provided for ABI compatibility, should calls
to it ever actually be compiled with future revisions of GCC?

I mean, since it's an implementation-only function, there should
be no code outside of libstdc++ that makes direct calls to it.
Code that did make calls to it (e.g., as a result of macro or
inline expansion) was presumably compiled into object files by
older versions of GCC (before the deprecation and before the
addition of the attribute) and won't benefit from the const
attribute now.  And since the function is deprecated, no code
newly compiled with GCC should make calls to it, either directly
or otherwise.  (Would using attribute deprecated on the function
make sense?)

The reason for my question is to understand if the warning is
justified (it's based on the documentation of attribute const:
"It does not make sense for a const function to return void.")

If it does make sense to declare a function const that returns
void then I'll remove the warning and update the manual and
mention this use case.  Otherwise, if you confirm that the
function shouldn't be called in new code I'll submit a patch
to remove the const attribute.

I think you're right that it won't be called from new code.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]