How to make Visual C ++ warn about silently converting int to string?

How do I get a compiler (vc14) to warn about this? I understand why this happens (int is silently converted to char and then to string, because it has a char constructor). But his source of errors, and he bit me more than once. Is there anything that can be done?

    int i = 1;
    std::string s;
    s = i; //"\x1"
+4
source share
4 answers

I wrote a comment, but I think it should be expanded to the correct answer:

std::stringcan be assigned char, so there is no way around this.

, , int char, .

, , C4244 (. 3 4 )

: https://msdn.microsoft.com/en-us/library/th7a07tz(v=vs.110).aspx

+1

4 (/W4), , .

C4244: "": "int" "char",

Visual ++ 3.

, , (, , ), (/Wall), , ( ). . " " Visual ++ MSDN

SAL /analyze. . C/++

+2

-Wconversion

:

#include <string>

int main() {
        int i = 1;
        ::std::string s;
        s = i;
        return 0;
}

Compilation Results :

g++ -Wconversion foo.cpp 
foo.cpp: In function 'int main()':
foo.cpp:6:4: warning: conversion to 'char' from 'int' may alter its value [-Wconversion]
  s = i;
    ^
+1
source

In my opinion, you should include all warnings by adding flags -Wall -Wextraat compile time.

You can also try the flag -Wconstant-conversion.

+1
source

Source: https://habr.com/ru/post/1626102/


All Articles