Ad
  • Custom User Avatar

    Converting to string is the worst solution. Not smart, not fast, not lightweight

  • Custom User Avatar

    small string optimisation means that this should (probably?) never allocate on the heap.
    I dont think this answer is best practice, but this comment thread starter is about as mislead as they thought the answer was.

  • Default User Avatar

    I'm not sure about it... It's an example of how one usually should not do it. Alternatives are simple enough and I don't really like using strings for maths. At the same time it solves the given problem in the allocated time which is the ultimate goal. From this point of view it doesn't look that bad.
    And by the way I'm not even sure if it uses heap at all: the size of string object itself is enough to store quite a few characters in place of pointer, capacity and length and modern implementations really do that.

  • Custom User Avatar

    std::string is heap allocated, so std::to_string makes allocations .

    converting things to strings is a relatively heavyweight operation as well... converting a number to a string to check a numeric property is a code smell.

    its very bad, even for a junior or lacking experience... the sort of thing you might send around the office as a joke (i've seen exactly this with stack overflow answers using this class of approach)

  • Default User Avatar

    Where's exactly allocation from the heap going?

  • Custom User Avatar

    I am totally agreed with semiessessi. It's a shame to call this piece of **** best practice.

  • Custom User Avatar

    ... at the risk of being rude, this is a /worst/ practice solution. it allocates memory from the heap for a start. :(

    upvoters should feel some shame for speaking without knowing what they are talking about.