Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Very nice article, however in 2022, why using C arrays as parameters in C++ code, when std::span, std::vector exist?


std::vector would definitely be the wrong choice, since I typically want to pass random subsections of random arrays to be filled by audio samples. std::span would be a good choice, though (I actually have my own span in the engine, from before C++20). Why didn't I use it? No idea, just didn't come to my mind. Guess it might be the influence of the book I was reading, or of SDL_Mixer interface, etc. I may do some refactoring in the future and use spans instead.


Thanks for the feeback.

I only raised the issue, because this is how new programmers keep doing mistakes in C++ when learning the code from others, specially projects that they find are cool to learn from.

As you seem to use best practices in the rest of the code that seemed strange to me.


SDL takes array pointers as arguments for its audio output functions in general, so I would assume they were coding against the SDL Audio API.


No reason to use them on C++ method definitions.

    void engine::callback(std::span<float> output)
    {
        std::size_t samples = 0;
        if (stream_)
        {
            samples = stream_->read(output.data(), output.size());
            if (samples == 0) stream_ = nullptr;
        }
        auto remaining = output.subspan(samples);
        std::fill(remaining.begin(), remaining.end(), 0.f);
    }


std::span is new enough to not be in muscle memory of C++ programmers.

Even the standard library commits a similar sin with `from_chars` and not taking a `string_view`.


std::span is new, but I have seen some custom form of it in pretty much every codebase I've worked with for a very long time (and implemented some myself). Like it used be the case for std::string in older codebases.


string_view would definitely have been wrong.

std::span did not exist in the Standard at the time std::from_chars was adopted.

An overload that takes std::span would be easy, and harmless. But as std::from_chars is only ever used from within some other abstraction, any benefit would be minimal.


Why would string_view be wrong there?


Because, then, to parse out a number you would first need to get it into a string.




Consider applying for YC's Winter 2026 batch! Applications are open till Nov 10

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: