samedi 21 septembre 2019

Implementation of std::rank for custom type (own class)

I tried to call std::rank on custom Foo type, it didn't work. I am guessing now that it works only on base type, and I need to extend class Foo to use it. https://en.cppreference.com/w/cpp/types/rank suggests possible implementation.

I am still learning C++ and I don't have much experience with more advanced topics. Could you tell me how can I add std::rank to Foo?

#include <iostream>
#include <type_traits>

class Foo {   
};

    template<class T>
    struct Foo::rank : public std::integral_constant<std::size_t, 0> {};

    template<class T>
    struct Foo::rank<T[]> : public std::integral_constant<std::size_t, rank<T>::value + 1> {};

    template<class T, std::size_t N>
    struct Foo::rank<T[N]> : public std::integral_constant<std::size_t, rank<T>::value + 1> {};

int main()
{
    Foo foo1[5];
    Foo foo2[5][5];

    std::cout << std::rank<Foo>(foo1);
    std::cout << std::rank<Foo>(foo2);

}

Aucun commentaire:

Enregistrer un commentaire