Boost logo

Boost :

Subject: Re: [boost] [Concepts] Definition. Was [GSoC] [Boost.Hana] Formal review request
From: Robert Ramey (ramey_at_[hidden])
Date: 2014-08-05 13:39:12


Krzysztof Czainski wrote
> What about this example:
>
> struct Circ
> {
> unsigned x;
> explicit Circ(unsigned i=0) : x(i%256) {}
> };
> bool operator<(Circ a, Circ b) { return (b.x-a.x)%256 < (a.x-b.x)%256; }
>
> Is std::set
> <Circ>
> valid?
>
> I think generally not, but... suppose that we know, that we're only going
> to use values 0-127. In this case std::set
> <Circ>
> is valid.

it won't trap at compile time. Of course depending upon the value of x it
might not be valid at runtime - but "type requirements" don't pretend to
address that. Type requirements specify what types are permitted at for
template parameters - no more no less. It can't guarantee that any
particular instance is "valid" (whatever that might mean).

Runtime checking is a job of "pre-conditions" and assert.

But if you want - you "could" go a little overboard and use

template<int X>
struct Circ
{
   static unsigned x = X;
};
bool operator<(Circ a, Circ b) { return (b.x-a.x)%256 < (a.x-b.x)%256; }

Of course this is outside the scope of "type requirements" but it's still
interesting.

Robert Ramey

--
View this message in context: http://boost.2283326.n4.nabble.com/Concepts-Definition-Was-GSoC-Boost-Hana-Formal-review-request-tp4666011p4666089.html
Sent from the Boost - Dev mailing list archive at Nabble.com.

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