简体   繁体   中英

How to initialize a template sized array?

I want to initialize a template sized array of objects with no default constructors, as shown in following code :

#include <array>

template<std::size_t N>
class Foo
{
    public:
        class Bar
        {
                Foo<N> & _super;

            public:
                Bar(Foo<N> *super) :
                    _super(*super)
                {
                }
        };

        std::array<Bar, N>  _array;

        Foo(void) :
            _array{{}} // We need {this, ...} N times
        {
        }
};


int main(void)
{
    Foo<3>  foo;
    (void)foo;
    return 0;
}

Is it a way to say : "I want an array of N objects, all initialized with this same parameter" ? I think there is a way with the template meta programming, but I cannot figure how to do it.

Everything is possible with the little help from make_index_sequence :

   Foo() : Foo(std::make_index_sequence<N>()) {} 
   template <size_t... I> Foo(std::index_sequence<I...> ) : _array{((void)I, this)...} {}

Notice the comma operator (,) in the _array constructor - courtesy of @Quentin (as opposed to function call).

You could just keep adding one this at a time until you have N of them, at which point you just initialize _array :

    Foo()
    : Foo(this)
    { }

private:
    template <class... T, std::enable_if_t<(sizeof...(T) < N), void*> = nullptr>
    Foo(T... args)
    : Foo(args..., this)
    { }

    template <class... T, std::enable_if_t<(sizeof...(T) == N), void*> = nullptr>
    Foo(T... args)
    : _array{{args...}}
    { }  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM