Irfy
Irfy

Reputation: 9587

Can we declare a struct as conforming to a concept?

With concepts we can require template parameters to conform to a concept, e.g. in:

template<ForwardIterator A>
struct S { A a; };

we parametrize the struct S with a type A which we require to conform to the concept ForwardIterator. Then we can instantiate S<std::vector<int>::iterator> but not, for example S<std::vector<int>>.

My question pertains to the definition of a concrete type T that would be used in place of A in the above case: *Can we declare a type T to conform to ForwardIterator already at the time of its definition, without instantiating S<T>?

Upvotes: 2

Views: 1315

Answers (1)

Nicol Bolas
Nicol Bolas

Reputation: 473407

A concept definition creates constant expression template. Since an instantiation of a concept is constant expression, you can use it in a static_assert statement. So if you have declared some type T and at some point want to verify that it fits into a concept C based on all accessible declarations, then you can do static_assert(C<T>);.

Of course, many concepts are not so simple. Many concepts constrain multiple parameters; they express the relationship between multiple types or whatever. You shouldn't limit your thinking about concepts in such an OOP/inheritance fashion.

Upvotes: 3

Related Questions