コンパイラの警告 (レベル 3) C4267

'var': 'size_t' から 'type' への変換です。データが失われる可能性があります。

注釈

コンパイラは、 size_t から値を切り捨てる可能性があるより小さい型への変換を検出しました。

この警告を解決するには、size_t の代わりに type を使用してください。 または、ターゲット プラットフォーム上の size_t の全範囲を表すことができる整数型を使用します。 コンパイル時に値が既知で、変換先の型に収まる場合、コンパイラはこの警告を生成しません。

例

次の例では、C4267 エラーが生成されます。

// C4267.cpp
// compile by using: cl /W4 C4267.cpp
void Func1(short) {}
void Func2(int) {}
void Func3(long) {}
void Func4(size_t) {}

int main() {
   size_t bufferSize = 10;
   Func1(bufferSize);   // C4267 for all platforms
   Func2(bufferSize);   // C4267 only for 64-bit platforms
   Func3(bufferSize);   // C4267 only for 64-bit platforms
   Func4(bufferSize);   // OK for all platforms

   // Because the compiler knows the value of bufferSize_const at compile time (because it's const),
   // it knows that converting it to short can't truncate the value so no warning is emitted
   const size_t bufferSize_const = 10;
   Func1(bufferSize_const); // No C4267

   // Because the compiler knows the value of bufferSize_big_const is 0x10000 at compile time
   // (because its const), it knows it doesn't fit in short, but does fit in int.
   const size_t bufferSize_big_const = 0x10000;
   Func1(bufferSize_big_const); // C4267
   Func2(bufferSize_big_const); // No C4267

   return 0;
}