0

There is a simple a program:

#include <iostream>
int main()
{
    int i = 1;
    double x = 2.5;
    x =i; // I do not want this can be compiled with no warning
    // I want only x=(double)i, or x=std::static_cast<double>i, that can compiled with no warning.
    std::cout << "x = " << x << std::endl;
    return 0;
}

I do not want implicit conversion from int to double in my program.

I want the compiler show warning unless I explicitly told the compiler it should be a conversion.

I compiled the program with GNU compiler g++ or intel C++ compiler icpc, there is no warning.

g++ main.cpp -Wall
icpc main.cpp -Wall

Is there something I can configure to implement the strict type conversion checking?

I am working in C++11, the C solution is also appreciated.

Thanks for your time.

tadman
  • 208,517
  • 23
  • 234
  • 262
Xu Hui
  • 1,213
  • 1
  • 11
  • 24
  • 2
    This cannot be done in C++, it does not work this way. – Sam Varshavchik Oct 24 '20 at 02:45
  • 1
    Your best bet is probably a custom clang-tidy check. This warning isn't something that you'd be likely to find in a compiler for everyone's use. – chris Oct 24 '20 at 02:52
  • 1
    Is it all conversions, or just conversions that may alter a value? gcc has `-Wconversion` (see the docs: https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html). And you can turn the warning into a hard error with `-Werror=conversion`. That doesn't apply for `int` -> `double`, because that's always losslessly possible, but it may help for other cases – Justin Oct 24 '20 at 02:56
  • 3
    You can absolutely do what you want ... with a *class*. That's why classes *exist*: so you can define the exact "semantics" of a "type"! – paulsm4 Oct 24 '20 at 03:00
  • @paulsm4, yes sir, using class give the int/double application's meaning, and define operator with class can implement that. – Xu Hui Oct 24 '20 at 03:02
  • It might help if you explain *why* you want to disallow this. Smells very much like an XY problem. – Mark Ransom Oct 24 '20 at 03:03
  • @MarkRansom Sir, I just want to be more strict. more strict checking, more safe. – Xu Hui Oct 24 '20 at 03:05
  • 1
    But why? There's a reason the language allows implicit conversion - a double can hold most integers without any problems. Do you have a specific case in mind where the conversion would cause a failure of some kind? I'm having trouble coming up with a situation where this wouldn't be safe. – Mark Ransom Oct 24 '20 at 03:11
  • @Mark Ransom Sir, for example, In my case, In a for loop, with iteration value `i`, I actually want to do `a[i] = b[i]`, where `a` and `b` is a double array. However, I miswrite to `a[i] = i`. This leads to a bug I find for a long time. If strict checking can happen, I will find the bug much quicker. So I come here to find if there exists a strict checking. – Xu Hui Oct 24 '20 at 03:20
  • 2
    That's the kind of error that most people would only make once in their life. I don't think it's worth spending a lot of time or effort trying to get the compiler to catch it for you; any solution is going to result in code that's harder to write and read, with a correspondingly higher chance of containing bugs. – Mark Ransom Oct 24 '20 at 03:28
  • 1
    Well if you are really serious about very strict and safe language, then C++ is far from being a good choice. By the way, in C++ there are a lot more gotchas one should be aware (There is even a book on that bu Duwhurst). The specific case above is rarely a problem in practice. If one need good safety then custom classes would be the solution if he cannot used a more appropriate language. – Phil1970 Oct 24 '20 at 04:09
  • https://stackoverflow.com/questions/10262983/make-gcc-warn-implicit-conversions – Tarik Oct 24 '20 at 04:51

1 Answers1

3

I believe you are looking for these warning flags:

-Wconversion -Warith-conversion

-Warith-conversion

    Do warn about implicit conversions from arithmetic operations even when conversion of the operands to the same type cannot change their values. This affects warnings from -Wconversion, -Wfloat-conversion, and -Wsign-conversion.

    void f (char c, int i)
    {
      c = c + i; // warns with -Wconversion
      c = c + 1; // only warns with -Warith-conversion
    }

https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#Warning-Options

EDIT:

If this isn't enough for you then you might be running into a bit of a wall.

But there are possible alternatives.

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Res-narrowing

There is a coreguideline that covers narrowing conversion issues.

While coreguidelines aren't generally covered by compilers other static analyzer tools might be up your alley.

Clang-tidy: https://clang.llvm.org/extra/clang-tidy/checks/cppcoreguidelines-narrowing-conversions.html

Cpp-core-check: https://devblogs.microsoft.com/cppblog/c-core-check-in-visual-studio/

If none of these are the solution then what you are looking it as completely legal conversions that are specified by C++. Because everything I'm describing/linking is to narrowing conversions. If your code won't cause a narrowing conversion then I don't believe any tool will currently catch this (as far as I know). Because it is completely legal C++.