Re: How to issue a warning if a C++ template is defined as 'inline' or 'static'

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Mon, 7 Oct 2024 at 10:29, Jonathan Wakely <jwakely.gcc@xxxxxxxxx> wrote:

>
>
> On Mon, 7 Oct 2024 at 10:10, R. Diez via Gcc-help <gcc-help@xxxxxxxxxxx>
> wrote:
>
>> Hi all:
>>
>> Sometimes, when I convert a function to a C++ template, I forget to
>> remove the 'static' or 'inline' attributes:
>>
>> template< typename IntegerType >
>> static inline  // Unnecessary
>> IntegerType my_template ( IntegerType v )
>> {
>>    return v;
>> }
>>
>> int main ( void )
>> {
>>    return my_template( 123 );
>> }
>>
>> Such 'static' and 'inline' attributes do not make sense for a template,
>> do they? Or do they actually have an effect?
>>
>
> They definitely have an effect.
>
> Exactly the same effects as on non-templates. 'static' gives it internal
> linkage, so the function has a different identity in every object. 'inline'
> makes it an inline function, which increases the probability that GCC will
> choose to inline the function when optimizing. The 'inline' specifier also
> allows a function to be defined in multiple objects, without getting
> redefinition errors - that aspect of 'inline' is not needed for function
> templates, because they can always be defined in multiple objects.
>
> (Why are you using 'static inline' on the non-template functions in the
> first place? Why not just 'inline'?)
>

In other words, stop using 'static inline' unnecessarily and just use
'inline', and then you don't need to change anything when making it a
template.


>
>
>> Is there a way to make GCC issue warnings about them? At the very least,
>> it would save some head scratching during code reviews.
>>
>> Thanks in advance,
>>    rdiez
>>
>



[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux