CppPilot

Compiler diagnostic Visual Studio C++

C4267 conversion from 'size_t', possible loss of data

In x64 builds, size_t has 64 bits and int 32. The warning appears when a size, such as vector::size() or strlen, is stored in an int.

Applies toMSVC on Windows; code and debugger concepts apply to MSBuild and CMake
ExamplesFocused excerpts; required context is stated alongside each example

Visual Studio property-page paths describe MSBuild projects. For CMake, configure the corresponding target or preset. Menu names can vary by Visual Studio version; use the cited reference for your toolset.

Build output
warning C4267: 'initializing': conversion from 'size_t' to 'int', possible loss of data

Causes & corrections

Find the case that matches your build. Each example shows what changes and why.

  1. A size or an index is stored in int.

    Use size_t, or auto: for (size_t i = 0; i < items.size(); ++i), or a range-based for.

    Problem

    std::vector<int> items{1, 2, 3};int count = items.size();

    Correction

    std::vector<int> items{1, 2, 3};auto count = items.size();
    Note 1With vector included, auto preserves the container's size type on x86 and x64.
    VideoFilmed in Visual Studio: this case fixed by hand, then with CppPilot. English narration, with subtitles in English, French, German, Italian and Spanish.
    Transcript

    C4267, conversion from size_t, possible loss of data: fixed by hand, then with CppPilot. In x64, size() returns a 64-bit size_t, and count is a 32-bit int.

    The project treats warnings as errors, so C4267 stops the build. Store the size in the type that holds it: declare count as size_t.

    Build again: nothing is truncated, and the build succeeds. Now the same error, fixed by CppPilot.

    After the failed build, select the error in the Error List, and click Fix with CppPilot. The card is the build error C2220, which turns the C4267 warning into an error.

    Click Fix on it. The AI keeps the type returned by size(): count becomes const auto, so nothing is truncated.

    CppPilot rebuilds the project to check the fix: the error is resolved. CppPilot explains and fixes C++ build errors in Visual Studio.

  2. An API needs an int or a DWORD.

    Check that the size fits, then cast explicitly: static_cast<int>(text.size()).

    Problem

    // std::string text;int length = text.size();

    Correction

    // Include <limits>, <stdexcept>, and <string>.if (text.size() > static_cast<std::size_t>((std::numeric_limits<int>::max)()))    throw std::length_error("text too long");int length = static_cast<int>(text.size());
    Note 2Check the size before converting to an API's int length. Parentheses also avoid the Windows max macro.

Sources and scope

Official references

These links document the compiler, build system, or debugger behavior discussed here. Use the version selector on Microsoft Learn for your toolset. Example blocks are focused excerpts; required headers and build settings are stated alongside them.

In your workspace

Investigate C4267 with CppPilot

CppPilot gives the selected AI the diagnostic, relevant code, and project settings for the active configuration. Choose Fix to request a correction, then check the result with a rebuild.

Use your own Codex or Claude account, or optional CppPilot Cloud access. Setup instructions

Visual Studio Marketplace