Boost logo

Boost :

Subject: Re: [boost] [contract] static_assert in contracts or not?
From: Andrzej Krzemienski (akrzemi1_at_[hidden])
Date: 2012-09-03 03:48:58


>
> Based on your and Vicente's input, I'll remove static_assert from contracts
> (pre, post, inv, etc).
>
> Is any predicate programmable with static_assert also programmable with
> Boost.ConceptCheck? I guess so if the Boost.ConceptCheck concept can do the
> static assertion using Boost.MPL or Boost.StaticAssert. If that it true
> then no need for me to provide static_assert at all, just use
> Boost.ConceptCheck. Otherwise, I'll allow static_assert together with
> Boost.ConceptCheck concepts in the requires clausle. How does that sound?
>

I do not know if Boost.ConceptCheck offers the capability of verifying any
boolean predicate. I know that concepts proposal (N2081) offered a special
concept True for that purpose (not sure if it is definable in
Boost.ConceptCheck). As a side note, in the new attempt at concept design
described in N3331, any concept is a boolean predicate by definition; thus
you can constraint template like this:

  template <typename T>
  requires InputIterator<T> && sizeof(T) > sizeof(void*)
  ...

I believe that whatever the answer to your question is, your idea to enable
static asserts along concept checking adds some value (let others judge ho
much): with concept checks you can only eliminate your template (or
template specialization) from overload resolution set and see what happens:
perhaps another overload will be picked and the compilation will succeed.
With static assertions, on the other hand, your template is _always_ always
preferred only to trigger compilation error the second after: this is often
useful when you want to provide clear error messages and you know there
will be no other overload. For instance, if I write a binary number
literal, and I want to specify at compile-time that my literal can only
accept 0s or 1s, I would never use concepts or sfinae tricks:

  template <char... C> requires Only0sAnd1s<C...>::value
  constexpr unsigned operator "" _B();

Instead, I would prefer to write:

  template <char... C>
  constexpr unsigned operator "" _B()
  {
    static_assert(
      Only0sAnd1s<C...>::value,
      "only 0s and 1s allowed in binary literal"
    );
    return ...
  }

Regards,
&rzej


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk