Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: spurious warning?

17 views
Skip to first unread message

Marcel Mueller

unread,
Nov 6, 2018, 2:55:09 AM11/6/18
to
Am 06.11.2018 um 02:50 schrieb Stefan Ram:
> I wrote the code quoted below.
> I hope one can get my intentions clearly from the code.
>
> What I'd like to ask about is this warning:
>
> main.cpp: In function 'bool is_nontrivial(int, int)':
> main.cpp:20:6: warning: function might be candidate for attribute 'const' [-Wsuggest-attribute=const]
> 20 | bool is_nontrivial( int const i, int const j )
> | ^~~~~~~~~~~~~
>
> Does this warning make sense for a /non-member/ function?

Not really.

> bool is_nontrivial( int const i, int const j )
> { return i != j && i > 1 && j > 1; }

But you can declare this function as constexpr. But this applies to
other function as well.

> Also feel free to suggest any improvements to my code!

From the C++ point of view I see nothing wrong.

From the performance point of view one could mention that f() is called
unnecessarily often. Currently 2 * imax² + (number of results) calls.
First of all you could eliminate the trivial cases by checking for them
first. Secondly the factor 2 can be eliminated by storing the result in
a variable to print the selfsame value rather than recalculating it.
- Nothing important for this small program, but you asked for potential
improvements.
Also it makes little sense to loop from 0 to imax (10) when you discard
any values less than 2 later in the is_nontrivial function. But I guess
this is just a test case.

From the mathematical point of view there are significantly more
efficient ways to check whether x²/y² is integral for integral x and y.
First of all x²/y² is exactly integral if x/y is integral. And to check
whether x is a multiple of y no floating point hack is required at all.
The check is simply x % y == 0.
Furthermore the result is for sure fractional if x < y (and x != 0 which
you ensured). So is_nontrivial could exclude everything but y > 1 && x > y.


Marcel

Chris Vine

unread,
Nov 6, 2018, 1:24:26 PM11/6/18
to
On 6 Nov 2018 01:50:02 GMT
r...@zedat.fu-berlin.de (Stefan Ram) wrote:
> I wrote the code quoted below.
> I hope one can get my intentions clearly from the code.
>
> What I'd like to ask about is this warning:
>
> main.cpp: In function 'bool is_nontrivial(int, int)':
> main.cpp:20:6: warning: function might be candidate for attribute 'const' [-Wsuggest-attribute=const]
> 20 | bool is_nontrivial( int const i, int const j )
> | ^~~~~~~~~~~~~
>
> Does this warning make sense for a /non-member/ function?
>
> Also feel free to suggest any improvements to my code!

[snip]

This warning seems to be concerned with gcc function attributes rather
than the C and C++ keyword 'const'. If so, it signifies that the
function does not read any global data and only depends on its
arguments:
https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#Common-Function-Attributes

It looks as if you have specifically asked for this "warning" (it isn't
really a warning) with the -Wsuggest-attribute=const compiler flag.
0 new messages