Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hexify() function emits conversion warning #270

Closed
dtoma opened this issue Jun 24, 2016 · 3 comments
Closed

hexify() function emits conversion warning #270

dtoma opened this issue Jun 24, 2016 · 3 comments
Milestone

Comments

@dtoma
Copy link
Contributor

dtoma commented Jun 24, 2016

Hello,

Using gcc 6.1.0.

Compilation error (-Wconversion -Werror):

./deps/json.hpp:9995:25:   required from here
./deps/json.hpp:5957:72: error: conversion to 'char' from 'int' may alter its value [-Werror=conversion]
                             : ('a' + static_cast<char>((v - 10) & 0x1f));
                                                                        ^
./deps/json.hpp:5957:72: error: conversion to 'char' from 'int' may alter its value [-Werror=conversion]

hexify:

const auto hexify = [](const int v) -> char
{
  return (v < 10)
  ? ('0' + static_cast<char>(v))
  : ('a' + static_cast<char>((v - 10) & 0x1f));
};

Could be written as something like:

const auto hexify = [](const int v) -> char
{
    static const std::array<char, 16> hex = { '0', '1', '2', '3',
                                              '4', '5', '6', '7',
                                              '8', '9', 'a', 'b',
                                              'c', 'd', 'e', 'f' };
    return hex[v];
};

Would you be interested in a PR for such a change?

@nlohmann
Copy link
Owner

Hi @dtoma, thanks for reporting. Yes, a PR would be great!

@nlohmann
Copy link
Owner

Hi @dtoma, if you like, I can add the change myself.

@nlohmann nlohmann added this to the Release 2.0.2 milestone Jun 28, 2016
@dtoma
Copy link
Contributor Author

dtoma commented Jun 29, 2016

Hi, I'll do it tonight, just got my dev environment setup.

nlohmann added a commit that referenced this issue Jul 1, 2016
Update hexify to use array lookup instead of ternary (#270)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants