1

For example

template<size_t N>
class A
{
    array<int, N> m;
    static A const UNIT {1, 1, ...}; // repeated N times, 
                                     // but I can't because of currently unspecified N
}

How to initialize a template sized array with the custom value 1?

3
  • Not sure, but if I see ... in conjunction with templates, I start thinking of using variadic template parameter lists and unpacking. Don't know how to generate these though, using a non type template parameter giving their number. Commented Jun 9, 2016 at 23:23
  • fill does some work. But I don't know how to put it. may be with lambda??? Commented Jun 9, 2016 at 23:26
  • 1
    Ah, well. Yes sure that does it. I was premising you were looking for a compile time solution. Commented Jun 9, 2016 at 23:36

1 Answer 1

2

You can use the fill function. This works fine with a static const member, too.

template<size_t N>
class A {
    array<int, N> m;
    public:
    static A const unit;
    A() { m.fill(1); }
};

template<size_t N>
A<N> const A<N>::unit{};
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.