Alexander Bily
Alexander Bily

Reputation: 965

Static assert in template parameters

I would like to ask if it is possible to insert static assert into template parameters.

Let´s say I want to create class StaticArray<T,N> and I want to make it impossible for users to instantiate this class with size equal to 0. Is there any way to insert something like static_assert(N != 0, "error message") into my class?

Upvotes: 0

Views: 2050

Answers (1)

Piotr Skotnicki
Piotr Skotnicki

Reputation: 48447

template <typename T, std::size_t N>
class StaticArray
{
    static_assert(N != 0, "error message");
};

LIVE DEMO

Upvotes: 6

Related Questions