Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: std::size_t instead of unsigned int  (Read 8784 times)

0 Members and 1 Guest are viewing this topic.

cob59

  • Newbie
  • *
  • Posts: 21
    • View Profile
std::size_t instead of unsigned int
« on: November 10, 2014, 10:57:38 pm »
Hi :)
Just a quick suggestion about the types used for indexing.

I'm compiling a VC++ projects in 64bits right now and I've noticed some sf::VertexArray methods use the 32bits unsigned int (i.e. unsigned long) index types, even though their underneath std::vector<Vertex> uses the 64bits unsigned long long types.

This leads to unnecessary casts and warnings.

So I think you should use std::size_t for unsigned indexes and std::ptrdiff_t for the signed ones.

NB: I didn't check, but sf::VertexArray probably isn't the only concerned class.
« Last Edit: November 10, 2014, 10:59:51 pm by cob59 »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: std::size_t instead of unsigned int
« Reply #1 on: November 10, 2014, 11:22:21 pm »
Yes, there are indeed some places where unsigned int is used to refer to an array size or index. I found the following ones:
void          Shape::getPoint(unsigned int index) const
unsigned int  Shape::getPointCount() const
// ... and the derived classes' setters

              VertexArray::VertexArray(PrimitiveType type, unsigned int vertexCount)
void          VertexArray::resize(unsigned int vertexCount)
Vertex&       VertexArray::operator[] (unsigned int index)
const Vertex& VertexArray::operator[] (unsigned int index) const

void          RenderWindow::draw(const Vertex* vertices, unsigned int vertexCount,
                  PrimitiveType type, const RenderStates& states)
« Last Edit: November 10, 2014, 11:24:16 pm by Nexus »
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: std::size_t instead of unsigned int
« Reply #2 on: November 12, 2014, 08:03:38 am »
I also found this, but I'm currently not sure with what unsigned int would need to be consistent. There are places where SFML does use std::size_t for indices and array sizes, e.g. loadFromMemory() or sf::String methods.

What do other developers think? I could open a branch in case we agree to change the above-mentioned places to std::size_t.
« Last Edit: November 12, 2014, 08:05:43 am by Nexus »
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

minirop

  • Sr. Member
  • ****
  • Posts: 254
    • View Profile
    • http://dev.peyj.com
Re: std::size_t instead of unsigned int
« Reply #3 on: November 12, 2014, 01:04:25 pm »
but I'm currently not sure with what unsigned int would need to be consistent.
it should be std::vector<>::size_type, which in turn is std::size_t.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: std::size_t instead of unsigned int
« Reply #4 on: November 12, 2014, 05:54:50 pm »
No, I meant the other thread, where Laurent stated unsigned int is probably used for consistency reasons.

In other words, I'm waiting for arguments to keep unsigned int in the above-mentioned places ;)
Otherwise I'll start working on the branch soon.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: std::size_t instead of unsigned int
« Reply #5 on: November 12, 2014, 06:59:05 pm »
I'm ok for replacing them.
Laurent Gomila - SFML developer

Gambit

  • Sr. Member
  • ****
  • Posts: 283
    • View Profile
Re: std::size_t instead of unsigned int
« Reply #6 on: November 13, 2014, 03:06:09 am »
I know this isnt exactly related to the topic but if I'm not mistaken, int as a type is frown upon due to its 32/64 bit size on their respective architectures. Is there any change that they could be changed to the proper std::u/int8/16/32/64 types?

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: std::size_t instead of unsigned int
« Reply #7 on: November 13, 2014, 10:05:34 am »
No and no:

1. Int is very, very likely to be 4 bytes (and (signed) int is actually what SFML typedefs sf::Int32 to). The type that varies with the bitness of the CPU is long (except on VC++ where it's apparently always 4 bytes too).

2. SFML can't use this header's typedefs: http://www.cplusplus.com/reference/cstdint/ because it's from C++11 which SFML doesn't use (yet).
« Last Edit: November 13, 2014, 10:13:03 am by FRex »
Back to C++ gamedev with SFML in May 2023

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: std::size_t instead of unsigned int
« Reply #8 on: November 16, 2014, 01:47:02 pm »
cob59, you can check out the bugfix/size_t branch and see if the warnings disappear.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

cob59

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: std::size_t instead of unsigned int
« Reply #9 on: November 17, 2014, 12:30:14 am »
There's an oversight in one file:
void ConvexShape::setPoint(std::size_t index, const Vector2f& point)
// instead of
void ConvexShape::setPoint(unsigned int index, const Vector2f& point)
 

After fixing it, the SFML project compiles/installs himself correctly with MSVC12 x64, but produces some warnings.

Yet, the sf::VertexArray used in my project do not produce any warnings now.
« Last Edit: November 17, 2014, 10:19:33 am by cob59 »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: std::size_t instead of unsigned int
« Reply #10 on: November 17, 2014, 12:19:21 pm »
I amended the commit. What are the other warnings about? I'm aware of one in InputImpl.cpp about unused parameter, and one about taking the address of main in sfml-main, as well as long long warnings on g++ in -pedantic mode.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

cob59

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: std::size_t instead of unsigned int
« Reply #11 on: November 17, 2014, 02:08:35 pm »
There were problems with:
  • stb_image.h that uses int types as a difference result between 2 pointers (should be ptrdiff_t, but the whole thing is inside an extern "C", so I don't know...)
  • IpAddress.cpp that uses inet_addr() that seems to be deprecated for MSVC12 (warning C4996: 'inet_addr': Use inet_pton() or InetPton() instead or define _WINSOCK_DEPRECATED_NO_WARNINGS to disable deprecated API warnings)
  • RenderTarget.cpp(L280): vertexCount is a size_t but glDrawArrays needs a GLsizei (i.e. an int).
« Last Edit: November 17, 2014, 05:19:16 pm by cob59 »

 

anything