UDAs - Restrict to User Defined Types?
Andrej Mitrovic
andrej.mitrovich at gmail.com
Thu Nov 8 22:08:36 PST 2012
On 11/9/12, H. S. Teoh <hsteoh at quickfur.ath.cx> wrote:
> Then all input ranges will have to explicitly declare they are an input
> range thus:
Or:
Change definition of isInputRange to:
---
template isInputRange(T)
{
enum bool isInputRange = (__attributes(T, RequiresInputRangeCheck)
&& __attributes(T, IsInputRangeAttr))
|| is(typeof(
(inout int _dummy=0)
{
R r = void; // can define a range object
if (r.empty) {} // can test for empty
r.popFront(); // can invoke popFront()
auto h = r.front; // can get the front of the range
}));
}
---
and in your user module:
---
module foo;
@RequiresInputRangeCheck: // apply attribute to all declarations in this module
@IsInputRangeAttr struct MyRange { /* front/popFront/empty */ }
struct NotARange { /* front/popFront/empty defined but not designed to
be a range */ }
---
---
static assert(isInputRange!MyRange);
static assert(!(isInputRange!NotARange));
---
That way you keep compatibility with existing ranges and introduce
extra safety check for new types which want to be checked.
More information about the Digitalmars-d
mailing list