2
#include <iostream>
#include <vector>
#include <cctype>

using namespace std;

char get_selection() {
    char selection{};
    cin >> selection;
    return toupper(selection);
}

int main() {
    char selection {};
    do{
        selection = get_selection();
        switch(selection){
           ...
        }
    } while(selection!='Q');

    cout<<endl;
    return 0;
}

I'd like to know why I get this check/warning/tip

Clang-Tidy: Narrowing conversion from 'int' to signed type 'char' is implementation-defined

The only thing I am doing there is getting the char and "uppercasing" it, in case it's not already, so I don't have to handle 2 cases on my switches. Does anybody know what I'd have to change, in order to get rid of this, so I'd get it completely green, since this is the only issue? It seems like I lack some knowledge regarding conversion.

Thanks!

printscreen

Martin Valgur
  • 5,793
  • 1
  • 33
  • 45
Kelv
  • 43
  • 1
  • 7
  • 1
    See the notes section: https://en.cppreference.com/w/cpp/string/byte/toupper – Alan Birtles Mar 29 '20 at 10:38
  • Man, I had already read my topics on this matter, but I wasn't reading the right ones. I now wrote return static_cast(toupper(static_cast(selection))); just like stated in the notes, and I got rid of that check. But ONLY because I saw how I should write it and just modified on my code - understand it, well, I didn't, since the notes are very short. We don't even get an explanation on why we got that message at all. The same code on a friend's shows no such message, but he uses codelite and c++17. So, there might be a version or IDE issue going on. Alan, thanks a lot. – Kelv Mar 29 '20 at 10:51

2 Answers2

2

In this function

char get_selection() {
    char selection{};
    cin >> selection;
    return toupper(selection);
}

the compiler makes implicit conversion from the type int, the return type of the C function toupper, to the type char.

To avoid the warning make explicit conversion for example like

char get_selection() {
    char selection{};
    cin >> selection;
    return char( ::toupper( ( unsigned char )selection) );
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Aha, it has to do with the C-function toupper! Now it makes more sense and using this code indeed solves the matter. So, I will read more about these functions, this way I'll know what it might be about, when I get suchlike messages, because I didn't know int was the the return type of the C function toupper - I've just learned with you. I wonder if C++ have this is upperthings as a method, like we could write return selection.upper; or sth like that. Thanks for this answer, Vlad! – Kelv Mar 29 '20 at 11:01
  • @Kelarov The type char is not a user-defined type. So it has no methods. – Vlad from Moscow Mar 29 '20 at 11:03
  • Aha. So the methods are for arrays, vectors, etc... - Objects, right? Now it seems to make more sense haha! Methods are so nice haha. I didn't know they couldn't be used with char types haha. We live, we learn! Thanks a lot, Vlad. – Kelv Mar 30 '20 at 12:38
2

Another solution that doesn't rely on implementation-defined behaviour is:

return toupper(selection, std::locale());

This version of toupper returns the same type as the input.

M.M
  • 138,810
  • 21
  • 208
  • 365
  • Man, that's truuuue! It works! The same check thing even suggested removing the #include header. It seems like the header was specific to this implementation/toupper/C-function. Thanks a lot for letting me know of this approach. – Kelv Mar 30 '20 at 12:36