mercredi 21 juillet 2021

Replacement for non-static non-const variable templates

I've just opened a question about this but unfortunately, as @super pointed out in his answer, defining arr static in the function getArray is not what I want. I would like something like a non-static non-const variable template but that specifically is not supported in c++ up until now. Is there some workaround for this which has the same effect as a non-static non-const variable template.

EDIT

This would be how I would expect the syntax to be if it was supported:

#include <array>


template<typename T_, size_t size_>
struct arg
{
    using T = T_;
    static constexpr size_t size = size_;
};

template<typename... Arugments>
struct Foo
{
    template<typename Argument>
    std::array<typename Argument::T, Argument::size> arr;
    
    template<typename Argument>
    std::array<typename Argument::T, Argument::size>& getArray() // specializations of all args in Arguments should be generated
    {
        return arr<Argument>;
    }
};

int main()
{
    Foo<arg<int, 10>> myFoo;
    myFoo.getArray<arg<int, 10>>()[0] = 1;
    Foo<arg<int, 10>> myFoo2;
    myFoo2.getArray<arg<int, 10>>()[0] = 2; // should not affect the arr of myFoo
}

Aucun commentaire:

Enregistrer un commentaire