Skip to content
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

Span with range check #237

Open
HenningScheufler opened this issue Jan 12, 2025 · 5 comments
Open

Span with range check #237

HenningScheufler opened this issue Jan 12, 2025 · 5 comments
Labels
enhancement New feature or request vote

Comments

@HenningScheufler
Copy link
Collaborator

HenningScheufler commented Jan 12, 2025

Span is currently unable to do range checks and this features will be added in 26

https://en.cppreference.com/w/cpp/container/span

I would propose the following:

using NeoFOAM::span = std::span

for debug builds, we would use our on implementation with range checks in the operator[]

What are your toughts do you support the idea?

@greole @gregorweiss @bevanwsjones @MarcelKoch

@HenningScheufler HenningScheufler added enhancement New feature or request vote labels Jan 12, 2025
@bevanwsjones
Copy link
Collaborator

I would further say that most containers we make should to quite 'heavy' debug checking in 'debug' mode, then in production it all gets turned off. I would add to the list quite extensive executor compatibility checking, though a separate issue. So I support this very strongly!

@greole
Copy link
Contributor

greole commented Jan 12, 2025

I think I agree fully, but could you elaborate what you mean by

we would use our on implementation with range checks in the operator[]

@HenningScheufler
Copy link
Collaborator Author

HenningScheufler commented Jan 12, 2025

something like this

namespace NeoFOAM
{

// in case of debug use DebugSpan instead of span
using span = std::span;

template<typename T>
class DebugSpan : public std::span<T>
{
public:

    using std::span<T>::span; // Inherit constructors from std::span

    DebugSpan(std::span<T> span) : std::span<T>(span) {}

    constexpr T& operator[](std::size_t index) const
    {
        if (index >= this->size())
        {
            Kokkos::abort("Index out of range in DebugSpan.\n");
        }
        return std::span<T>::operator[](index);
    }
};


} // namespace NeoFOAM

@MarcelKoch
Copy link
Collaborator

I think it would be fine if the check can be disabled for release builds.

@greole
Copy link
Contributor

greole commented Jan 13, 2025

Like this?

namespace NeoFOAM
{

// in case of debug use DebugSpan instead of span
using span = std::span;

template<typename T>
class Span : public std::span<T>
{
public:

    using std::span<T>::span; // Inherit constructors from std::span

    constexpr T& operator[](std::size_t index) const
    { 
    #ifdef DEBUG
        if (index >= this->size())
        {
            Kokkos::abort("Index out of range in DebugSpan.\n");
        }
    #endif
        return std::span<T>::operator[](index);
    }
};


} // namespace NeoFOAM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request vote
Projects
None yet
Development

No branches or pull requests

4 participants