GithubHelp home page GithubHelp logo

Comments (1)

dmitrykobets-msft avatar dmitrykobets-msft commented on May 28, 2024 1

This appears to be expected behavior.

If you replace gsl::span with std::span in your example, the behavior is actually undefined:

#include <array>
#include <span>

void f() {
    std::array<char, 20> a{};
    gsl::span<char> s1{a};
    gsl::span<char, 10> s2{s1.first(10)}; 
    gsl::span<char, 10> s3{s1};  // undefined behavior
}

This is because when constructing a std::span of static extent from a std::span of dynamic extent

The behavior is undefined if both extent != dynamic_extent and source.size() != extent are true.

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

When running that code with MSVC in debug mode, you actually get a runtime error "Cannot construct span with static extent from other span as other.size() != extent".

So this is a feature (defect?) of the std::span constructors.

... but ... since the behavior is technically "undefined" by std::span why can't we choose to implement it in a more convenient manner for gsl::span?
I imagine the choice to disallow it was made to keep the behavior consistent with how static-extent spans behave:

gsl::span<char, 20> s1{ a };
gsl::span<char, 19> x{ s1 }; // compile-time error, can't find appropriate constructor

std::span<char, 20> s2{ a };
std::span<char, 19> y{ s2 }; // compile-time error, can't find appropriate constructor

So, considering that fundamentally the design of gsl::span is such that it should match the interface of std::span exactly, while offering bounds checking, gsl::span raises the error you reported.

from gsl.

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.