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

use static_cast<Uint8> in Network/Packet.cpp #731

Closed
zoujiaqing opened this Issue Oct 28, 2014 · 4 comments

Comments

Projects
None yet
5 participants
@zoujiaqing

zoujiaqing commented Oct 28, 2014

////////////////////////////////////////////////////////////
Packet& Packet::operator <<(Int64 data)
{
    // Since htonll is not available everywhere, we have to convert
    // to network byte order (big endian) manually
    Uint8 toWrite[] =
    {
        static_cast<Uint8>((data >> 56) & 0xFF),
        static_cast<Uint8>((data >> 48) & 0xFF),
        static_cast<Uint8>((data >> 40) & 0xFF),
        static_cast<Uint8>((data >> 32) & 0xFF),
        static_cast<Uint8>((data >> 24) & 0xFF),
        static_cast<Uint8>((data >> 16) & 0xFF),
        static_cast<Uint8>((data >>  8) & 0xFF),
        static_cast<Uint8>((data      ) & 0xFF)
    };
    append(&toWrite, sizeof(toWrite));
    return *this;
}


////////////////////////////////////////////////////////////
Packet& Packet::operator <<(Uint64 data)
{
    // Since htonll is not available everywhere, we have to convert
    // to network byte order (big endian) manually
    Uint8 toWrite[] =
    {
        static_cast<Uint8>((data >> 56) & 0xFF),
        static_cast<Uint8>((data >> 48) & 0xFF),
        static_cast<Uint8>((data >> 40) & 0xFF),
        static_cast<Uint8>((data >> 32) & 0xFF),
        static_cast<Uint8>((data >> 24) & 0xFF),
        static_cast<Uint8>((data >> 16) & 0xFF),
        static_cast<Uint8>((data >>  8) & 0xFF),
        static_cast<Uint8>((data      ) & 0xFF)
    };
    append(&toWrite, sizeof(toWrite));
    return *this;
}
@jcowgill

This comment has been minimized.

Show comment
Hide comment
@jcowgill

jcowgill Oct 28, 2014

Contributor

If anyone was wondering - this is what you currently get if you compile with -Wall:

.../SFML/src/SFML/Network/Packet.cpp: In member function ‘sf::Packet& sf::Packet::operator<<(sf::Int64)’:
.../SFML/src/SFML/Network/Packet.cpp:441:22: warning: narrowing conversion of ‘(sf::Int64)(((long unsigned int)data) >> 56)’ from ‘sf::Int64 {aka long long int}’ to ‘sf::Uint8 {aka unsigned char}’ inside { } is ill-formed in C++11 [-Wnarrowing]
         (data >> 56) & 0xFF,
Contributor

jcowgill commented Oct 28, 2014

If anyone was wondering - this is what you currently get if you compile with -Wall:

.../SFML/src/SFML/Network/Packet.cpp: In member function ‘sf::Packet& sf::Packet::operator<<(sf::Int64)’:
.../SFML/src/SFML/Network/Packet.cpp:441:22: warning: narrowing conversion of ‘(sf::Int64)(((long unsigned int)data) >> 56)’ from ‘sf::Int64 {aka long long int}’ to ‘sf::Uint8 {aka unsigned char}’ inside { } is ill-formed in C++11 [-Wnarrowing]
         (data >> 56) & 0xFF,

@Bromeon Bromeon added this to the 2.2 milestone Oct 28, 2014

@LaurentGomila

This comment has been minimized.

Show comment
Hide comment
@LaurentGomila

LaurentGomila Oct 28, 2014

Member

Can you please edit your message to add at least a small description of the issue, instead of just throwing code and let people figure out what's going on?

Member

LaurentGomila commented Oct 28, 2014

Can you please edit your message to add at least a small description of the issue, instead of just throwing code and let people figure out what's going on?

@Bromeon

This comment has been minimized.

Show comment
Hide comment
@Bromeon

Bromeon Oct 28, 2014

Member

True... this is one of the places where C++11 is backwards-incompatible.

A meaningful problem description would still be appropriate. Thanks for the clarification, @jcowgill

Member

Bromeon commented Oct 28, 2014

True... this is one of the places where C++11 is backwards-incompatible.

A meaningful problem description would still be appropriate. Thanks for the clarification, @jcowgill

@eXpl0it3r

This comment has been minimized.

Show comment
Hide comment
@eXpl0it3r

eXpl0it3r Oct 29, 2014

Member

I assume these static_casts are what we'll do right?

Member

eXpl0it3r commented Oct 29, 2014

I assume these static_casts are what we'll do right?

@eXpl0it3r eXpl0it3r self-assigned this Oct 29, 2014

@eXpl0it3r eXpl0it3r removed the s:unassigned label Oct 29, 2014

@eXpl0it3r eXpl0it3r closed this in c2761c3 Nov 3, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment