FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Test on static rank · Issue #2135 · xtensor-stack/xtensor · GitHub

Test on static rank #2135

Closed
Closed

Description

I'm currently overloading based on rank. E.g.,

void foo(xt::xtensor<double,2>& a);
void foo(xt::xtensor<double,4>& a);

I would like to make the API more flexible for example to use xt::xtensor_fixed<double, xt::xshape<3, 3>>. Is there a way to use SFINAE to overload based on rank, such that I can allow a very generic template?

Activity

  1. JohanMabille commented on Sep 6, 2020

    Member

    You can rely on SFINAE but you'll have to specialize the "testing" function for different types. For instance:

    template <class E, size_t N>
    struct has_rank : std::false_type
    {
    };
    
    template <class T>
    struct has_rank<xt::xtensor<T, 2>, 2>
        : std::true_type
    {
    };
    
    template <class T, size_t N1, size_t N2>
    struct has_rank<xt::xtensor_fixed<T, xt::xshape<N1, N2>>>
        : std::true_type
    {
    };
    
    template <class E>
    std::enable_if_t<has_rank<E, 2>> foo(E& e)
    {
        // ...
    }

    This is going to be really cumbersome. Another solution would be to add a rank property to the different classes of xtensor, so the testing function can work for any kind of expression:

    template <class E, size_t N>
    struct has_rank
    {
        using type = std::integral_constant<bool, E::rank == N>;
    };
    
    template <class E, size_t N>
    using has_rank_t = typename has_rank<E, N>::type;
  2. tdegeus commented on Sep 7, 2020

    MemberAuthor

    Thanks @JohanMabille ! The latter would be a really nice solution for me, and I guess something that could be beneficial for others. Could be a great extension for the next release. I will submit a PR today or tomorrow that we can iterate on.

  3. self-assigned this
    on Sep 7, 2020
  4. tdegeus commented on Sep 7, 2020

    MemberAuthor

    @JohanMabille What would you say that E::rank should be for xarray? Or should there be a second variable: has_fixed_rank?

  5. JohanMabille commented on Sep 7, 2020

    Member

    I think we could use SIZE_MAX for a dynamic rank.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


    Back | FazBrowse Home | New Git URL