Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #93779

Re: How do I set this type variable?

From David Brown <david.brown@hesbynett.no>
Newsgroups comp.lang.c
Subject Re: How do I set this type variable?
Date 2016-09-19 09:34 +0200
Organization A noiseless patient Spider
Message-ID <nro4ed$iht$1@dont-email.me> (permalink)
References <9f7fba02-df47-4d06-aac3-27c01758c3e3@googlegroups.com> <nrhlbq$ocb$1@dont-email.me> <eecdb610-bfa0-4180-9303-10045d5a0940@googlegroups.com> <349c65aa-49e2-401a-ba29-faa48c276fd9@googlegroups.com> <e499ntFtdveU2@mid.individual.net>

Show all headers | View raw


On 19/09/16 07:00, Ian Collins wrote:
> On 09/18/16 05:47 PM, supercat@casperkitty.com wrote:
>> On Saturday, September 17, 2016 at 7:19:21 PM UTC-5, Malcolm McLean
>> wrote:
>>> You have two policies. One is "const correctness", whereby everything
>>> is const unless the function actually needs to change it. The
>>> other is rigorous elimination and deprecation of const as an
>>> undesirable programming construct.
>>>
>>> The problems are created in between.
>>
>> Many programming languages fail to recognize the concepts of a readable
>> pointer, a read-only pointer, and a pointer to an immutable object.  All
>> three of them could be characterized as a "SomeType const *", but they
>> should have distinct meanings.
>>
>> Some libraries accept pointers from client code and later give those same
>> pointers back to client code.  The libraries won't write through the
>> pointers, but won't care if client code does so with the originals or the
>> passed-back copies.  Such pointers should be "readable".
>>
>> Some libraries accept pointers from client code and pass them on to other
>> code, and the copies of the pointers should never be written through. 
>> The
>> supplier of the pointers, however, may retain the right to write through
>> them, especially after the called function returns.  Such pointers should
>> be "read-only".
>>
>> Some libraries may cache information read through pointers they receive,
>> and may malfunction if any code ever writes through them.  The targets of
>> such pointers should be "immutable".
>>
>> Some ambiguities with pointer qualifiers could be resolved by having a
>> "return" qualifier, such that e.g.
>>
>>      char *strchr(char return const *src, int match);
>>
>> would indicate that if the first past argument has a const qualifier,
>> the return value should be regarded as having one also, but if the
>> source argument does not have one, the return value shouldn't either.
>> Had such a qualifier been available when strchr was defined, that would
>> have avoided a lot of difficulties with functions like strchr.  It would
>> not solve all such difficulties, especially in cases where a library
>> needs to store passed-in pointers between function calls, but it would
>> have helped many.
> 
> Function overloading is a more elegant solution for this.
> 

In C, you have:

	char *strchr(const char *s, int c);

In C++, function overloading means you have:

	const char* strchr(const char* s, int c);
	char* strchr(char* s, int c);

Function overloading gives you a const-safe system for this, but it is
/not/ elegant in cases like this.  In particular, it is pure code
duplication - the bodies of the two functions will be identical except
for the "const".  That means either you write two copies of the
function, or you use templates (with extra code to limit it to just
these two types), or you have to write a wrapper function with
const_cast for one of them.

With Supercat's idea, you could have just one function

	char* strchr(return_const char* src, int c);

One function declaration, one function definition, and const consistency
and safety.

I am sure the details would get complicated (in either C or C++), but
the idea is not bad at all.

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

How do I set this type variable? Thiago Adams <thiago.adams@gmail.com> - 2016-09-16 13:08 -0700
  Re: How do I set this type variable? BartC <bc@freeuk.com> - 2016-09-16 21:40 +0100
    Re: How do I set this type variable? Thiago Adams <thiago.adams@gmail.com> - 2016-09-16 13:57 -0700
      Re: How do I set this type variable? Thiago Adams <thiago.adams@gmail.com> - 2016-09-16 14:07 -0700
      Re: How do I set this type variable? jacobnavia <jacob@jacob.remcomp.fr> - 2016-09-16 23:32 +0200
        Re: How do I set this type variable? Thiago Adams <thiago.adams@gmail.com> - 2016-09-17 12:05 -0700
          Re: How do I set this type variable? BartC <bc@freeuk.com> - 2016-09-17 20:46 +0100
    Re: How do I set this type variable? Malcolm McLean <malcolm.mclean5@btinternet.com> - 2016-09-17 17:19 -0700
      Re: How do I set this type variable? supercat@casperkitty.com - 2016-09-17 22:47 -0700
        Re: How do I set this type variable? luser droog <luser.droog@gmail.com> - 2016-09-18 13:44 -0700
        Re: How do I set this type variable? Ian Collins <ian-news@hotmail.com> - 2016-09-19 17:00 +1200
          Re: How do I set this type variable? David Brown <david.brown@hesbynett.no> - 2016-09-19 09:34 +0200
            Re: How do I set this type variable? supercat@casperkitty.com - 2016-09-19 08:18 -0700
  Re: How do I set this type variable? Richard Damon <Richard@Damon-Family.org> - 2016-09-23 12:07 -0400
    Re: How do I set this type variable? Tim Rentsch <txr@alumni.caltech.edu> - 2016-09-23 11:57 -0700

csiph-web