1

The C++ compiler (gcc version 4.4.7 20120313 (Red Hat 4.4.7-23.0.1) (GCC)) doesn't issue expected warning when converting long long to int (see the code example below).

// compiler: gcc version 4.4.7 20120313 (Red Hat 4.4.7-23.0.1) (GCC)
// compiled with flags -pedantic -Wextra -Wshadow -Wall -Wconversion -std=gnu++0x

#include <stdio.h>

class AConv {
    public:
    long long z() {
        return 7634843234L;
    }
    void p() {
        const int v = z(); // NO WARNING. WHY ? See next line which calls the same function.
        const int w = this->z(); // compiler warning: conversion to 'int' from 'long long int' may alter its value
        printf( "v=%d, w=%d\n", v, w); // butchered values shown
    }
};

I find this behavior counter-intuitive.
Is it a g++ - only feature?
I'd like to force the compiler to generate warning for both invocations, not just for the second one:

v = z();
w = this->z();

I cannot upgrade the compiler (I work in a corporate environment with constraints).

  • 2
    Your compiler is more than seven years old. Check it with a newer version. – Sid S Dec 17 '19 at 00:27
  • You are not going to find much support for GCC 4.4 nowadays. Are you working on a Red Hat system? They are infamous for supplying antique software. Is it possible to use Fedora 31 or Ubuntu 18? They supply modern compilers. Also, you should use std streams rather then `printf` with C++. – jww Dec 17 '19 at 00:29
  • Fwiw printf is a variadic function and doesn't care about what you pass it. If you tell it it is an int, and it isn't you get undefined behavior. – NathanOliver Dec 17 '19 at 00:49

2 Answers2

1

In GCC world, -Wall is not enough to get such warnings. -Wconversion is needed.

EDIT:
Some tests at https://wandbox.org/ tell me that GCC 4.4.7 does not provide this warning. The first version that provides it is 4.7.3.

Ripi2
  • 7,031
  • 1
  • 17
  • 33
  • I'm using -Wconversion (see comments at the beginning of the source code): // compiled with flags -pedantic -Wextra -Wshadow -Wall -Wconversion -std=gnu++0x – Peter Dzimko Dec 17 '19 at 00:56
  • Thank you for your GCC release advice. I compiled the snippet with GCC 4.8.4 and the compiler duly generates conversion warnings for both problematic lines. – Peter Dzimko Dec 20 '19 at 14:02
0

You need the postfix 'LL' for long long. You've just specified long (a smaller type)

return 7634843234LL;
robthebloke
  • 9,331
  • 9
  • 12