How can I fix an int-to-bool warning in C ++?

I get a warning in MSVC ++ when I try to read an integer from a file and make the bool variable equal to it.

accessLV[i] = FileRead(file1, i + 1);

(accessLV is an array of bools, FileRead is a function that I did to reduce the syntax involved in reading from a file, I - because the statement is in a for loop)

I tried using static_cast:

accessLV[i] = static_cast<bool>(FileRead(file1, i + 1));

But I still get a warning. I tried to do this (I'm not sure what this term is):

accessLV[i] = (bool)FileRead(file1, i + 1));

And the warning still exists. Is there a way to get rid of the warning without making accessLV an array of ints?

NB: this is FileRead syntax if it helps:

int FileRead(std::fstream& file, int pos)
{
    int data;
    file.seekg(file.beg + pos * sizeof(int));
    file.read(reinterpret_cast<char*>(&data), sizeof(data));
    return data;
}
+3
source share
4 answers

What about

accessLV[i] = FileRead(file1, i + 1) != 0;
+8

,

accessLV [i] = (FileRead (file1, + 1)!= 0)

+3
accessLV[i] = FileRead(file1, i + 1) != 0;

int bool: , accessLV [i], .

+2

, !=0 - , . โ€‹โ€‹, :

// myutil.hpp
template< typename T >
inline bool bool_cast( const T & t ) { return t != 0; }

:

// yourcode.cpp
accessLV[ i ] = bool_cast( FileRead( file1, i + 1 ) );

, .

+2

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


All Articles