www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - user defined no bounds check

reply "monarch_dodra" <monarchdodra gmail.com> writes:
There is a way to prevent bounds checking of array accesses when 
compiling. I was wondering if there was a way to have a user 
defined range apply the same scheme? I'd suppose using a 
"version"? I'm not very fluent with this yet...
Aug 20 2012
next sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On Monday, August 20, 2012 10:43:43 monarch_dodra wrote:
 There is a way to prevent bounds checking of array accesses when
 compiling. I was wondering if there was a way to have a user
 defined range apply the same scheme? I'd suppose using a
 "version"? I'm not very fluent with this yet...

I believe that your only options are assert, debug blocks, and version blocks. There's no way for code to no whether -noboundscheck is used just like there's no way for code to know whether -release is used. - Jonathan M Davis
Aug 20 2012
prev sibling next sibling parent "monarch_dodra" <monarchdodra gmail.com> writes:
On Monday, 20 August 2012 at 09:10:47 UTC, Jonathan M Davis wrote:
 On Monday, August 20, 2012 10:43:43 monarch_dodra wrote:
 There is a way to prevent bounds checking of array accesses 
 when
 compiling. I was wondering if there was a way to have a user
 defined range apply the same scheme? I'd suppose using a
 "version"? I'm not very fluent with this yet...

I believe that your only options are assert, debug blocks, and version blocks. There's no way for code to no whether -noboundscheck is used just like there's no way for code to know whether -release is used. - Jonathan M Davis

For my personal education, what is the rationale behind not being able to write: "version(noboundscheck)" or "version(release)" ? Is it purposefully done to force the implementer to provide his own version "word" and "switch"?
Aug 20 2012
prev sibling next sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On Monday, August 20, 2012 11:45:23 monarch_dodra wrote:
 On Monday, 20 August 2012 at 09:10:47 UTC, Jonathan M Davis wrote:
 On Monday, August 20, 2012 10:43:43 monarch_dodra wrote:
 There is a way to prevent bounds checking of array accesses
 when
 compiling. I was wondering if there was a way to have a user
 defined range apply the same scheme? I'd suppose using a
 "version"? I'm not very fluent with this yet...

I believe that your only options are assert, debug blocks, and version blocks. There's no way for code to no whether -noboundscheck is used just like there's no way for code to know whether -release is used. - Jonathan M Davis

For my personal education, what is the rationale behind not being able to write: "version(noboundscheck)" or "version(release)" ? Is it purposefully done to force the implementer to provide his own version "word" and "switch"?

I'm not sure that it was ever even considered that the programmer might want to do something special with them. assert is affected by -release, and - noboundscheck was added later for extra control. They had nothing to do with the programmer doing anything in their code. I have no idea how Walter would feel about adding noboundscheck and/or release versions which correspond to the flags. But since it's generally bad practice to make code differ between non-release mode and release mode, and assert already gives you the ability to have checks that which are around in non-release but not in release, I don't know that it really buys you much to add such version identifiers. - Jonathan M Davis
Aug 20 2012
prev sibling next sibling parent "monarch_dodra" <monarchdodra gmail.com> writes:
On Monday, 20 August 2012 at 09:58:54 UTC, Jonathan M Davis wrote:
 I'm not sure that it was ever even considered that the 
 programmer might want
 to do something special with them. assert is affected by 
 -release, and -
 noboundscheck was added later for extra control. They had 
 nothing to do with
 the programmer doing anything in their code. I have no idea how 
 Walter would
 feel about adding noboundscheck and/or release versions which 
 correspond to
 the flags.

 But since it's generally bad practice to make code differ 
 between non-release
 mode and release mode, and assert already gives you the ability 
 to have checks
 that which are around in non-release but not in release, I 
 don't know that it
 really buys you much to add such version identifiers.

 - Jonathan M Davis

C++ uses "ifdef debug" a lot, so I guess it is confusing at first, but "assert" can indeed get the same done (worst case scenario, it can call a function with side effects that always returns true). There is no equivalent for "ifndef debug", but I agree that is bad. Regarding "noboundscheck" I was thinking in particular about Container.Array, or any other range in general. If the user compiles using noboundscheck, then one can only assume the user does not want for Container.Array.Range to check its own bounds either. Andrei says something in his book along the lines of "If a built in can do it, so should a user defined type" (don't have the quote here though, sorry). So basically, if a built in array can conditionally control bounds checking, why can't a user defined range do it?
Aug 20 2012
prev sibling next sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On Monday, August 20, 2012 12:44:40 monarch_dodra wrote:
 Andrei says something in his book along the lines of "If a built
 in can do it, so should a user defined type" (don't have the
 quote here though, sorry). So basically, if a built in array can
 conditionally control bounds checking, why can't a user defined
 range do it?

Except that generally assert does the job just fine. The only reason that - noboundscheck is any different really is the fact that bounds checking is left on in safe code even with -release. It just seems overkill to me try and have user-defined types act the same way. It's arguably bad enough that arrays work this way. If you want control user-defined types with a version(noboundscheck), then create an enhancement request, and maybe Walter will think that it's a good idea. I don't know. It seems completely unnecessary to me, but that doesn't mean that it won't happen. - Jonathan M Davis
Aug 20 2012
prev sibling parent "monarch_dodra" <monarchdodra gmail.com> writes:
On Monday, 20 August 2012 at 15:42:50 UTC, Jonathan M Davis wrote:
 On Monday, August 20, 2012 12:44:40 monarch_dodra wrote:

 Except that generally assert does the job just fine. The only 
 reason that -
 noboundscheck is any different really is the fact that bounds 
 checking is left
 on in  safe code even with -release. It just seems overkill to 
 me try and have
 user-defined types act the same way. It's arguably bad enough 
 that arrays work
 this way.

 If you want control user-defined types with a 
 version(noboundscheck), then
 create an enhancement request, and maybe Walter will think that 
 it's a good
 idea. I don't know. It seems completely unnecessary to me, but 
 that doesn't
 mean that it won't happen.

 - Jonathan M Davis

Oh yeah... noboundscheck is not as simple as just doing "-noboundscheck". You are probably right, this would bring more harm than good.
Aug 20 2012