Topic: How do I check a class satisfies a concept?


Author: BetaNona <betanona@gmail.com>
Date: Sun, 3 May 2009 10:10:23 CST
Raw View
Hello,

Assume I want to use this Useful class template, and to make Bar class
for using in Useful's template argument.

template<Foo T> class Useful{/* ... */};

But Foo concept is tremendous complex. Therefore I want to check on
ahead that my Bar satisfies Foo.

I come up with some simple statements, but they all are invalid.

Foo Bar{ /*...*/ }; //No! It's not a declaration.
class Bar requires Foo<Bar>{ /*...*/ }; //No!! Non-template class
can't use requires-clause.
class Bar : Foo<Bar>{/*...*/}; //No!!! Foo<Bar> is not a base class.
class Bar{ requires Foo<Bar>; /*...*/ }; //No!!!! requires is not a
member.

This way is OK at least, but ugly, indirect, and messy... (If Bar must
satisfy 10 concepts? there are 100 Bar-like classes?)

class Bar{/*...*/};
template<Foo T> class checkFoo{};
static_assert(sizeof(checkFoo<Bar>));

Is there a simpler way?

--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: daniel.kruegler@googlemail.com
Date: Sun, 3 May 2009 13:30:21 CST
Raw View
On 3 Mai, 18:10, BetaNona <betan...@gmail.com> wrote:
> Hello,
>
> Assume I want to use this Useful class template, and to make Bar class
> for using in Useful's template argument.
>
> template<Foo T> class Useful{/* ... */};
>
> But Foo concept is tremendous complex. Therefore I want to check on
> ahead that my Bar satisfies Foo.
>
> I come up with some simple statements, but they all are invalid.
>
> Foo Bar{ /*...*/ }; //No! It's not a declaration.
> class Bar requires Foo<Bar>{ /*...*/ }; //No!! Non-template class
> can't use requires-clause.
> class Bar : Foo<Bar>{/*...*/}; //No!!! Foo<Bar> is not a base class.
> class Bar{ requires Foo<Bar>; /*...*/ }; //No!!!! requires is not a
> member.
>
> This way is OK at least, but ugly, indirect, and messy... (If Bar must
> satisfy 10 concepts? there are 100 Bar-like classes?)
>
> class Bar{/*...*/};
> template<Foo T> class checkFoo{};
> static_assert(sizeof(checkFoo<Bar>));
>
> Is there a simpler way?

The most direct way which comes into my mind
would be to define an empty Foo concept map
immediately after the definition of Bar:

concept Foo<typename T> { ... }

class Bar { ... };

concept_map Foo<Bar>{};

Greetings from Bremen,

Daniel Kr   gler


--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]