You can replace the last line with:

morph->search_list.emplace_back( morph->dictionary.size() - 1, 0, 0, 0 );

Thus the object is created not through brace initialization which does not allow narrowing conversion.

The narrowing conversion is from the return value of the call to size which returns std::size_t which is unsigned.

For why size() - 1 is not converted to a signed value see: C++ Implicit Conversion (Signed + Unsigned)

Answer from Amir Kirsh on Stack Overflow
🌐
Arduino Forum
forum.arduino.cc › projects › programming
warning: narrowing conversion of '-1' from 'int' to 'unsigned int' inside { } [- - Programming - Arduino Forum
August 10, 2018 - Hi all, I've googled this but other issues I see are all related to converting int to char. I'm just assigning integers to unsigned int. This is an example code replicating the issue: unsigned int rawCodes[3] = {0, 1, -1}; Any negative values drop this error: warning: narrowing conversion of ...
Discussions

Warning: narrowing conversion from 'int' to 'uint8_t {aka unsigned char}' inside { } [-Wnarrowing]
Hi, I'm working on "LCDWIKI_SPI" library for ST7796S TFT 4" display. It compiles and working. But I'm getting this warning: void LCDWIKI_SPI::Set_Addr_Window(int16_t x1, int16_t y1, int16_t x2, int16_t y2) { uint8_t… More on forum.arduino.cc
🌐 forum.arduino.cc
1
0
September 22, 2022
c++ - Narrowing conversion from int to unsigned char - Stack Overflow
Your a and b are being promoted to int or unsigned int, added, and then converted back down to unsigned char. If the conversion is expected program behavior (from your point of view as the designer), you can explicitly cast it to the type you want. An explicit cast is not a narrowing conversion. More on stackoverflow.com
🌐 stackoverflow.com
Narrowing Conversion In Codeforces Problem (
Your posts seem to contain unformatted code. Please make sure to format your code otherwise your post may be removed. If you wrote your post in the "new reddit" interface, please make sure to format your code blocks by putting four spaces before each line, as the backtick-based (```) code blocks do not work on old Reddit. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
🌐 r/cpp_questions
7
2
January 27, 2025
c++ - error C2397: conversion from 'int' to 'unsigned int' requires a narrowing conversion - Stack Overflow
I have an std::array defined inside a class. I want to initalise it inside the constructor, like this: MyClass::MyClass(std::map const&data) { More on stackoverflow.com
🌐 stackoverflow.com
🌐
Learn C++
learncpp.com › cpp-tutorial › narrowing-conversions-list-initialization-and-constexpr-initializers
10.4 — Narrowing conversions, list initialization, and constexpr initializers – Learn C++
May 5, 2023 - However, you may have noticed that ... when it is “From an integral type to another integral type that cannot represent all values of the original type, unless the value being converted is constexpr and whose value can be stored exactly in the destination type.”...
🌐
Microsoft Learn
learn.microsoft.com › en-us › cpp › cpp › type-conversions-and-type-safety-modern-cpp
Type conversions and type safety | Microsoft Learn
June 18, 2025 - Notice that values are reinterpreted in both directions. If your program produces odd results in which the sign of the value seems inverted from what you expect, look for implicit conversions between signed and unsigned integral types. In the following example, the result of the expression ( 0 - 1) is implicitly converted from int to unsigned int when it's stored in num.
🌐
Reddit
reddit.com › r/cpp_questions › narrowing conversion in codeforces problem (
r/cpp_questions on Reddit: Narrowing Conversion In Codeforces Problem (
January 27, 2025 -

Hi,

This is my submission to this problem. I don't understand how the narrowing conversion goes, I don't know why the first time I use std::max it says there's narrowing conversion, but why? Isn't float, double, and long double are all integers with more presicon? Or is it that if they will have decimal numbers they will "sacrifice" whole numbers?

If it's my first guess then can you help to avoid that narrowing conversion? I tried to make them all float and double numbers and still the same answer. Or is it just my code is wrong...

I hope I explained my issue well.

Thanks.


  1. Edit: It seems the submission link doesn't work, so here's the code:

  2. Edit: And it once, is my coding right? Am I following the best practices?

#include <iostream>
#include <vector>
#include <algorithm>
 
 
int main() {
    int n, l;
    std::cin >> n >> l;
 
    if (n == 1) {
        std::cout << l / 2.0f;
    }
 
    std::vector<int> arr(n);
 
    for (int i{}; i < n; i++) {
        std::cin >> arr[i];
    }
 
    std::sort(arr.begin(), arr.end());
 
    float min_radius{ std::max(arr[0], l - arr[n - 1]) };
    int x, y;
 
    for (int i{ 1 }; i < n; i++) {
        x = arr[i - 1];
        y = arr[i];
 
        min_radius = std::max(min_radius, (y - x) / 2.0f);
 
        x = y;
    }
 
    std::cout << min_radius;
    
    return 0;
}

And the test checks are:

Input
7 15
15 5 3 7 9 14 0

Participant's output
2.5

Jury's answer
2.5000000000

Checker comment
ok found '2.500000000', expected '2.500000000', error '0.000000000'
Input
2 5
2 5

Participant's output
2

Jury's answer
2.0000000000

Checker comment
ok found '2.000000000', expected '2.000000000', error '0.000000000'
Input
46 615683844
431749087 271781274 274974690 324606253 480870261 401650581 13285442 478090364 266585394 425024433 588791449 492057200 391293435 563090494 317950 173675329 473068378 356306865 311731938 192959832 321180686 141984626 578985584 512026637 175885185 590844074 47103801 212211134 330150 509886963 565955809 315640375 612907074 500474373 524310737 568681652 315339618 478782781 518873818 271322031 74600969 539099112 85129347 222068995 106014720 77282307

Participant's output
2.22582e+07

Jury's answer
22258199.5000000000

Checker comment
wrong answer 1st numbers differ - expected: '22258199.5000000', found: '22258200.0000000', error = '0.0000000'
Find elsewhere
🌐
Stack Overflow
stackoverflow.com › questions › 61186776 › error-c2397-conversion-from-int-to-unsigned-int-requires-a-narrowing-conver
c++ - error C2397: conversion from 'int' to 'unsigned int' requires a narrowing conversion - Stack Overflow
2 error: narrowing conversion of ‘18446744069414584320ull’ from ‘long long unsigned int’ to ‘int’ inside { } [-Wnarrowing] 0 error: implicit conversion changes signedness: 'int' to 'unsigned long' 0 conversion from 'unsigned int' to 'int' requires a narrowing conversion
🌐
Stack Overflow
stackoverflow.com › questions › 51227263 › c-invalid-narrowing-conversion-from-int-to-unsigned-char
C++ - Invalid narrowing conversion from "int" to "unsigned char" - Stack Overflow
Make it static_cast<uint8_t>(224 + code / 16) You are adding uint8_t to an int - the result is of type int. ... Alternative you can make code a constexpr see C++11: “narrowing conversion inside { }” with modulus for explanation
Top answer
1 of 4
7

If practical do:

enum FunctionType
{
    NORMAL   = 0, 
    RANGE    = 1
};

typedef struct TableEntry
{
    FunctionType value;
    const char *label;
} TableEntry;


TableEntry functionTypes[] = 
{
    {NORMAL,   "NORMAL"},
    {RANGE, "RANGE"}
};

Otherwise, change the type in the struct to int, or explicitly base the enumeration on the same type as in the struct.

Btw., I the think g++ warning is unfounded and wrong, since the original code is valid C++03. Correction: As I understand it now, the diagnostic is correct, and this is a breaking change in C++11. I just couldn't believe it.


About the naming convention: those all uppercase identifiers are good for Java programmers (who are used to that convention), but in C++ they increase the chances of inadvertent text substitution. There are also the aesthetic considerations. Much win in reserving all uppercase for macros.

2 of 4
4

Usually underlying type of unscoped enumerations is int ( it can be any integral type that can represent all values of enumerators).

However I do not see any narrowing conversion because type unsigned long can represent all values of type int.

EDIT: It seems I am wrong because I found an example in the Standard that contradicts my assumption

unsigned int ui1 = {-1}; // error: narrows

So unsigned int can not be initialized by using an initializer list that contains a negative number.

So to avoid the warning the enumeration could be written as

enum FunctionType : unsigned int // or unsigned long
{
    NORMAL   = 0, 
    RANGE    = 1
};
🌐
Reddit
reddit.com › r/cpp_questions › help to avoid narrowing conversions in my code
r/cpp_questions on Reddit: Help to avoid narrowing conversions in my code
January 21, 2023 -

Hi Reddit,

Experienced programmer, that's new to C++ here. I'm trying to read a .txt file using C++ and store the resulting words into a vector, What I've tried so far is the following:

#include <iostream>
#include <fstream>
#include <string>
#include <vector>

int main() {
    int current_char;
    std::string current_word;
    std::vector<std::string> words{};
    std::ifstream my_file("data/The Complete Works of William Shakespeare by William Shakespeare.txt");

    if (my_file.is_open()) {
        while (my_file.good()) {
            current_char = my_file.get();
            if (std::ispunct(current_char)){
                words.push_back(current_word);
                current_word = "";
            }
            else {
                current_word.push_back(current_char);
            }
        }
    }
    else {
        std::cout << "File not opened" << std::endl;
    }

}

The warning that I'm getting is the following Clang-Tidy: Narrowing conversion from 'int' to signed type 'char' is implementation-defined. I've googled the warning and found the clang-tidy documentation that explains the warning, however I'm unsure what I should be doing instead.

Could anyone give me an explanation of how to avoid this / what to do about it?

Thanks

Ki1103

🌐
Arch Linux Forums
bbs.archlinux.org › viewtopic.php
Proper fix for narrowing conversion error / Programming & Scripting / Arch Linux Forums
November 19, 2016 - ... In my mind, casting a signed value to an unsigned variable is an error. Regardless, the conversion is to take the absolute value of the signed value, compliment each bit, and add one. The resultant value, of course, is dependent on the size of int on the system.
🌐
GitConnected
levelup.gitconnected.com › narrowing-conversions-in-c-0a256e90af91
Narrowing Conversions in C++. Detailed examples for narrowing… | by Pin Loon Lee | Level Up Coding
July 22, 2024 - “The rank of any unsigned integer ... floating point types, it would not be considered as narrowing conversion if conversion happens from integral type to another integral type with higher rank, with the condition that it ...
🌐
Microsoft Learn
learn.microsoft.com › en-us › cpp › error-messages › compiler-errors-1 › compiler-error-c2397
Compiler Error C2397 | Microsoft Learn
A narrowing conversion can be okay when you know the possible range of converted values can fit in the target. In this case, you know more than the compiler does. If you make a narrowing conversion intentionally, make your intentions explicit ...