1

If i have for example some template struct, and i want to give it a user-defined size for some member i can do it by passing a value to constructor like so:

template <typename T>
struct Foo {
    int m_size;

    Foo(int u_size)
        :
        m_size {u_size}
    {
    }
};

and i can also do it by having a non-type template parameter (kinda the way std::array does it), like so:

template <typename T, int u_size>
struct Foo {
    int m_size;

    Foo()
        :
        m_size {u_size}
    {
    }
};

My question is, what is the difference between these two methods, and when is it useful to use either of them?

9
  • 3
    For the non-type template parameter version it generally doesn't make sense to have another member variable. One would just use u_size for everything inside the class.
    – paleonix
    Jan 30 at 10:20
  • std::array needs its size to be known at compile time, it wouldn't work otherwise as it has something like T data[size]; as data member. So if it works for you with an constructor argument, that is probably (i.e. in most situations) the way to go.
    – paleonix
    Jan 30 at 10:22
  • @paleonix the member is not const Jan 30 at 10:25
  • 1
    @463035818_is_not_a_number yes, but having m_size and u_size differ at runtime seems like a bad idea. Having a const member is a very bad idea as well. And yes, one can probably find some weird use-case were these would be sensible things to do, but it would be exceedingly rare.
    – paleonix
    Jan 30 at 10:29
  • 1
    "yes, but having m_size and u_size differ at runtime seems like a bad" why? You can have a vector with initial size 2 and later you resize it to 42. This is not an example where you would make 2 a template parameter, but you cannot exclude that there is one where it would make sense Jan 30 at 10:30

1 Answer 1

2

You are using u_size to determine the initializer for the member m_size. The difference between the two examples boils down to Foo<T,i> being a different type than Foo<T,j>. Other than being instantiations of the same template (which usually does not mean much) they are totally unrelated. On the other hand, in the first case, any Foo<T> is of the same type Foo<T>, no matter what was the initial value for the member.

If m_size changes at runtime, it is questionable to require an instance that uses 42 as initial value and an instance that uses 24 as initial value to be of different types.

Maybe m_size does not change at runtime, then it can make sense to have the initial value as part of the type (like eg with std::array). Maybe it does change at runtime, but nevertheless there are reasons to have the initial value baked into the type. There is no "better" Or "worse", it depends what you want/need.

1
  • 1
    There are even types which have fixed value or a magic value to say the value is dynamic (as std::span.
    – Jarod42
    Jan 30 at 13:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.