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


Groups > comp.lang.c > #163210

Re: on the order of the const-keyword

From Keith Thompson <Keith.S.Thompson+u@gmail.com>
Newsgroups comp.lang.c
Subject Re: on the order of the const-keyword
Date 2021-10-28 15:37 -0700
Organization None to speak of
Message-ID <875ytg22ty.fsf@nosuchdomain.example.com> (permalink)
References (2 earlier) <a25df1f0-687b-4489-8f2f-6daa35593040n@googlegroups.com> <994066a9-5940-4d1c-8df4-517905174930n@googlegroups.com> <slf1ic$qcv$1@dont-email.me> <7697a67c-fc7e-4b7c-b53f-38076b50cde2n@googlegroups.com> <aadf2b60-6c41-434a-8c19-8fc2a60bff15n@googlegroups.com>

Show all headers | View raw


Thiago Adams <thiago.adams@gmail.com> writes:
> On Thursday, October 28, 2021 at 5:57:38 PM UTC-3, Thiago Adams wrote:
[...]
>> At the end it could work as enumerators work today 
>> 
>> enum { A = 1 }; 
>> void F(int* i) {} 
>> void F2(int i) {} 
>> int main() { 
>> F(&A); //error: lvalue required as unary '&' operand 
>> F2(A); //OK 
>> } 
>> 
>> sometimes a local variable would be created, except for int double etc..
>
> The reason why this features does not exist today may be because for complex 
> types we can create a const variable and reuse it. 
> and use defines for int double etc.

C's "const" keyword does not mean "constant", and it never has.  Of
course the spelling is derived from the English word "constant", which
can cause some understandable confusion.

A *constant expression* is one that can be evaluated at compile time.
A *constant* is a literal.

The keyword "const" might more accurately have been spelled "readonly".
If a name is const-qualified, it means that the named object cannot be
modified via that name.  Defining something as "const" never makes
its name a constant expression.

C++ added a special case, so that for example
    const int n = 42;
makes the expression n a constant expression (though n is still an
object whose address can be taken).  Later, C++ added the "constexpr"
keyword (like "const" but it requires the initializer to be constant),
which in my opinion would have been a better choice for this special
case.

Note that this:
    const time_t now = time(NULL);
is perfectly legal; the expression t cannot even in principle be
evaluated at compile time.  (`constexpr time_t now = time(NULL);`
is invalid in C++.)

I wouldn't mind seeing a future version of C adopt something like C++'s
"constexpr" -- *without* changing the current meaning of "const".

-- 
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Philips
void Void(void) { Void(); } /* The recursive call of the void */

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


Thread

on the order of the const-keyword Meredith Montgomery <mmontgomery@levado.to> - 2021-10-28 12:50 -0300
  Re: on the order of the const-keyword Bart <bc@freeuk.com> - 2021-10-28 17:25 +0100
    Re: on the order of the const-keyword "james...@alumni.caltech.edu" <jameskuyper@alumni.caltech.edu> - 2021-10-28 10:10 -0700
      Re: on the order of the const-keyword Guillaume <message@bottle.org> - 2021-10-28 20:40 +0200
      Re: on the order of the const-keyword Meredith Montgomery <mmontgomery@levado.to> - 2021-10-29 00:07 -0300
        Re: on the order of the const-keyword James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-10-29 00:48 -0400
    Re: on the order of the const-keyword Thiago Adams <thiago.adams@gmail.com> - 2021-10-28 12:56 -0700
      Re: on the order of the const-keyword Thiago Adams <thiago.adams@gmail.com> - 2021-10-28 13:18 -0700
        Re: on the order of the const-keyword Bart <bc@freeuk.com> - 2021-10-28 21:35 +0100
          Re: on the order of the const-keyword Thiago Adams <thiago.adams@gmail.com> - 2021-10-28 13:57 -0700
            Re: on the order of the const-keyword Thiago Adams <thiago.adams@gmail.com> - 2021-10-28 14:07 -0700
              Re: on the order of the const-keyword Bart <bc@freeuk.com> - 2021-10-28 22:54 +0100
              Re: on the order of the const-keyword Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-10-28 15:37 -0700
                Re: on the order of the const-keyword Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-10-29 00:02 +0100
        Re: on the order of the const-keyword Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-10-28 22:10 +0100
        Re: on the order of the const-keyword Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-10-28 15:13 -0700
        Re: on the order of the const-keyword Meredith Montgomery <mmontgomery@levado.to> - 2021-10-29 00:40 -0300
    Re: on the order of the const-keyword Meredith Montgomery <mmontgomery@levado.to> - 2021-10-28 23:48 -0300
  Re: on the order of the const-keyword James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-10-28 13:04 -0400
    Re: on the order of the const-keyword Meredith Montgomery <mmontgomery@levado.to> - 2021-10-29 00:34 -0300
  Re: on the order of the const-keyword David Brown <david.brown@hesbynett.no> - 2021-10-29 10:03 +0200
    Re: on the order of the const-keyword Bart <bc@freeuk.com> - 2021-10-29 11:30 +0100
      Re: on the order of the const-keyword David Brown <david.brown@hesbynett.no> - 2021-10-29 16:31 +0200
        Re: on the order of the const-keyword Bart <bc@freeuk.com> - 2021-10-29 16:45 +0100
          Re: on the order of the const-keyword David Brown <david.brown@hesbynett.no> - 2021-10-29 18:30 +0200
            Re: on the order of the const-keyword Manfred <noname@add.invalid> - 2021-10-30 00:32 +0200
              Re: on the order of the const-keyword Meredith Montgomery <mmontgomery@levado.to> - 2021-10-29 20:03 -0300
              Re: on the order of the const-keyword Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-10-30 01:10 +0100
            Re: on the order of the const-keyword Bart <bc@freeuk.com> - 2021-10-29 23:51 +0100
    Re: on the order of the const-keyword Meredith Montgomery <mmontgomery@levado.to> - 2021-10-29 12:22 -0300
      Re: on the order of the const-keyword David Brown <david.brown@hesbynett.no> - 2021-10-29 18:34 +0200
        Re: on the order of the const-keyword Meredith Montgomery <mmontgomery@levado.to> - 2021-10-29 17:04 -0300

csiph-web