Code Monkey home page Code Monkey logo

Comments (3)

sguazt avatar sguazt commented on June 11, 2024

Hi Juan,
I would also suggest to use the std::size_t type in place of the int type in methods like

  • int numberOfXXX(),
  • ... getXXX(int),
  • void setXXX(..., int)
  • ... removeXXX(int)

Some weeks ago, I prepared a specific branch in my fork

https://github.com/sguazt/fuzzylite/tree/patch/use-size_t

which however was based on a fork of your v5.x branch.
Currently I have no time for development, but maybe in the next months I can try to port the changes in the develop branch.

Let me know.
Cheers,

Marco

from fuzzylite.

jcrada avatar jcrada commented on June 11, 2024

Hi Marco,

thank you very much for your work! I will check if I can merge that into the develop branch.

However, could you elaborate a bit on why should I use return types of std::size_t instead of std::vector<T>::size_type? I really liked int because it is less verbose, but if this needs to be changed, then why not the std::vector<T>::size_type?

Cheers,
Juan.

from fuzzylite.

sguazt avatar sguazt commented on June 11, 2024

Hi,
The reason to use std::size_t in place of int is to avoid possible conversion errors.
Most of the containers used inside fuzzylite classes use an unsigned integral type to return the number of elements, while the int numberOfXXX() methods use a signed integral type. This means that if the number of elements stored in the container is greater than std::numeric_limits<int>::max() it is likely to run into an overflow problem.
Also note that std::size_t is usually implemented as unsigned long, so you got an additional problem in case sizeof(int) < sizeof(long).

Regarding your proposal to use std::vector<T>::size_type, personally I don't like it since:

  1. You expose to the user of the classes the internal details of your implementation. If one day, for some reason, you decide to use a container other than std::vector you'll have to change the public signature of your APIs to match the new_container::size_type type.
  2. As far as I know. the type std::size_t is really the standard and recommended way (in the C++ sense) to refer to types that represents a size (likewise, std::ptrdiff_t is the standard type to use to represent differences and offsets). From the C++14 draft standard (sec. 18.2):
    • The type size_t is an implementation-defined unsigned integer type that is large enough to contain the size in bytes of any object
    • The type ptrdiff_t is an implementation-defined signed integer type that can hold the difference of two subscripts in an array object

Obviously, this is my personal point of view

Cheers,

Marco

from fuzzylite.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.