public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* gcc warning about return value truncation
@ 2019-12-29  5:12 Yubin Ruan
  2019-12-29  6:28 ` Liu Hao
  0 siblings, 1 reply; 3+ messages in thread
From: Yubin Ruan @ 2019-12-29  5:12 UTC (permalink / raw)
  To: gcc-help

Is there any gcc flag to warn about this?

#include <iostream>
#include <cstdint>

int16_t func() {
    int32_t num = INT32_MAX;
    std::cout << "func() called " << num << std::endl;
    return num;
}

int main()
{
    int32_t val = func();
    std::cout << "return value is " << val << std::endl;
}

The return value of func() was truncated because the return type is
accidentally written as int16_t

Thanks,
Yubin

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: gcc warning about return value truncation
  2019-12-29  5:12 gcc warning about return value truncation Yubin Ruan
@ 2019-12-29  6:28 ` Liu Hao
  2020-01-02  5:37   ` Yubin Ruan
  0 siblings, 1 reply; 3+ messages in thread
From: Liu Hao @ 2019-12-29  6:28 UTC (permalink / raw)
  To: Yubin Ruan, gcc-help


[-- Attachment #1.1: Type: text/plain, Size: 333 bytes --]

在 2019/12/29 13:11, Yubin Ruan 写道:
> Is there any gcc flag to warn about this?
> 
> 
> The return value of func() was truncated because the return type is
> accidentally written as int16_t
> 


It's `-Wconversion`. By the way I suggest you add `-Wsign-conversion` in
C++ as well.


-- 
Best regards,
LH_Mouse


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: gcc warning about return value truncation
  2019-12-29  6:28 ` Liu Hao
@ 2020-01-02  5:37   ` Yubin Ruan
  0 siblings, 0 replies; 3+ messages in thread
From: Yubin Ruan @ 2020-01-02  5:37 UTC (permalink / raw)
  To: Liu Hao; +Cc: gcc-help

Thanks a lot.

On Sun, Dec 29, 2019 at 2:29 PM Liu Hao <lh_mouse@126.com> wrote:
>
> 在 2019/12/29 13:11, Yubin Ruan 写道:
> > Is there any gcc flag to warn about this?
> >
> >
> > The return value of func() was truncated because the return type is
> > accidentally written as int16_t
> >
>
>
> It's `-Wconversion`. By the way I suggest you add `-Wsign-conversion` in
> C++ as well.
>
>
> --
> Best regards,
> LH_Mouse
>

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2020-01-02  5:37 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-12-29  5:12 gcc warning about return value truncation Yubin Ruan
2019-12-29  6:28 ` Liu Hao
2020-01-02  5:37   ` Yubin Ruan

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).