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


Groups > comp.lang.c > #169181 > unrolled thread

lvalue assignment difference between C and C++

Started byMuttley@dastardlyhq.com
First post2023-02-04 15:30 +0000
Last post2023-02-10 16:19 -0800
Articles 20 on this page of 35 — 13 participants

Back to article view | Back to comp.lang.c


Contents

  lvalue assignment difference between C and C++ Muttley@dastardlyhq.com - 2023-02-04 15:30 +0000
    Re: lvalue assignment difference between C and C++ Richard Damon <Richard@Damon-Family.org> - 2023-02-04 11:04 -0500
      Re: lvalue assignment difference between C and C++ Muttley@dastardlyhq.com - 2023-02-04 16:59 +0000
        Re: lvalue assignment difference between C and C++ Richard Damon <Richard@Damon-Family.org> - 2023-02-04 13:02 -0500
        Re: lvalue assignment difference between C and C++ Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2023-02-04 12:45 -0800
        Re: lvalue assignment difference between C and C++ David Brown <david.brown@hesbynett.no> - 2023-02-05 11:42 +0100
          Re: lvalue assignment difference between C and C++ Muttley@dastardlyhq.com - 2023-02-06 09:26 +0000
            Re: lvalue assignment difference between C and C++ David Brown <david.brown@hesbynett.no> - 2023-02-06 14:13 +0100
              Re: lvalue assignment difference between C and C++ Muttley@dastardlyhq.com - 2023-02-06 15:50 +0000
                Re: lvalue assignment difference between C and C++ David Brown <david.brown@hesbynett.no> - 2023-02-06 20:08 +0100
                Re: lvalue assignment difference between C and C++ Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2023-02-06 12:47 -0800
            Re: lvalue assignment difference between C and C++ Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2023-02-06 13:03 -0800
              Re: lvalue assignment difference between C and C++ Muttley@dastardlyhq.com - 2023-02-07 09:24 +0000
                Re: lvalue assignment difference between C and C++ Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2023-02-07 11:08 -0800
                  Re: lvalue assignment difference between C and C++ Muttley@dastardlyhq.com - 2023-02-08 09:23 +0000
                  Re: lvalue assignment difference between C and C++ Blue-Maned_Hawk <bluemanedhawk@gmail.com> - 2023-02-08 22:13 -0500
                    Re: lvalue assignment difference between C and C++ James Kuyper <jameskuyper@alumni.caltech.edu> - 2023-02-08 22:53 -0500
                    Re: lvalue assignment difference between C and C++ Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2023-02-08 22:12 -0800
                Re: lvalue assignment difference between C and C++ Ben Bacarisse <ben.usenet@bsb.me.uk> - 2023-02-08 02:52 +0000
        Re: lvalue assignment difference between C and C++ Paavo Helde <eesnimi@osa.pri.ee> - 2023-02-06 09:11 +0200
          Re: lvalue assignment difference between C and C++ Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2023-02-06 01:01 -0800
            Re: lvalue assignment difference between C and C++ Richard Damon <Richard@Damon-Family.org> - 2023-02-06 07:42 -0500
              Re: lvalue assignment difference between C and C++ Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2023-02-06 12:59 -0800
          Re: lvalue assignment difference between C and C++ bart c <bart4858@gmail.com> - 2023-02-06 08:11 -0800
            Re: lvalue assignment difference between C and C++ Ben Bacarisse <ben.usenet@bsb.me.uk> - 2023-02-06 21:19 +0000
          Re: lvalue assignment difference between C and C++ Bonita Montero <Bonita.Montero@gmail.com> - 2023-02-07 07:44 +0100
            Re: lvalue assignment difference between C and C++ Paavo Helde <eesnimi@osa.pri.ee> - 2023-02-07 09:18 +0200
      Re: lvalue assignment difference between C and C++ Kaz Kylheku <864-117-4973@kylheku.com> - 2023-02-06 22:21 +0000
        Re: lvalue assignment difference between C and C++ Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2023-02-06 14:43 -0800
          Re: lvalue assignment difference between C and C++ Paul N <gw7rib@aol.com> - 2023-02-07 03:17 -0800
            Re: lvalue assignment difference between C and C++ Ben Bacarisse <ben.usenet@bsb.me.uk> - 2023-02-07 16:25 +0000
              Re: lvalue assignment difference between C and C++ Paul N <gw7rib@aol.com> - 2023-02-08 13:19 -0800
                Re: lvalue assignment difference between C and C++ Ben Bacarisse <ben.usenet@bsb.me.uk> - 2023-02-08 23:46 +0000
    Re: lvalue assignment difference between C and C++ Bonita Montero <Bonita.Montero@gmail.com> - 2023-02-04 19:41 +0100
    Re: lvalue assignment difference between C and C++ Andrey Tarasevich <andreytarasevich@hotmail.com> - 2023-02-10 16:19 -0800

Page 1 of 2  [1] 2  Next page →


#169181 — lvalue assignment difference between C and C++

FromMuttley@dastardlyhq.com
Date2023-02-04 15:30 +0000
Subjectlvalue assignment difference between C and C++
Message-ID<trltmg$21cft$1@dont-email.me>
Can anyone explain the "official" explanation from whichever standard as to
why the first assignment only compiles in C++ but not C:

#include <stdio.h>

int main()
{
        int a = 0;
        int b = 0;
        int x = 0;
        int y = 1;
        int c = 123;

        /* Compiles in C++, not C
        (x == y ? a : b) = c;
        */

        /* C version */
        *(x == y ? &a : &b) = c;

        printf("a = %d, b = %d\n",a,b);
        return 0;
}

Just curious.

[toc] | [next] | [standalone]


#169182

FromRichard Damon <Richard@Damon-Family.org>
Date2023-02-04 11:04 -0500
Message-ID<0mvDL.68810$b7Kc.34465@fx39.iad>
In reply to#169181
On 2/4/23 10:30 AM, Muttley@dastardlyhq.com wrote:
> Can anyone explain the "official" explanation from whichever standard as to
> why the first assignment only compiles in C++ but not C:
> 
> #include <stdio.h>
> 
> int main()
> {
>          int a = 0;
>          int b = 0;
>          int x = 0;
>          int y = 1;
>          int c = 123;
> 
>          /* Compiles in C++, not C
>          (x == y ? a : b) = c;
>          */
> 
>          /* C version */
>          *(x == y ? &a : &b) = c;
> 
>          printf("a = %d, b = %d\n",a,b);
>          return 0;
> }
> 
> Just curious.
> 

The key difference is that in C, the results of the conditional operator 
is always an r-value, and thus not suitable for an assignment operator.

By using the address-of and deference operatiors you convert that 
r-value into an l-value so can make the assignment.

In C++, because it has a "reference" type, if the last two terms of the 
conditional operator are objects of a compatible type, then the type of 
the result can be a reference to that type, an option not available in 
C, and this allows its value to be an l-value.

[toc] | [prev] | [next] | [standalone]


#169183

FromMuttley@dastardlyhq.com
Date2023-02-04 16:59 +0000
Message-ID<trm2tb$224hr$1@dont-email.me>
In reply to#169182
On Sat, 4 Feb 2023 11:04:12 -0500
Richard Damon <Richard@Damon-Family.org> wrote:
>On 2/4/23 10:30 AM, Muttley@dastardlyhq.com wrote:
>> Can anyone explain the "official" explanation from whichever standard as to
>> why the first assignment only compiles in C++ but not C:
>> 
>> #include <stdio.h>
>> 
>> int main()
>> {
>>          int a = 0;
>>          int b = 0;
>>          int x = 0;
>>          int y = 1;
>>          int c = 123;
>> 
>>          /* Compiles in C++, not C
>>          (x == y ? a : b) = c;
>>          */
>> 
>>          /* C version */
>>          *(x == y ? &a : &b) = c;
>> 
>>          printf("a = %d, b = %d\n",a,b);
>>          return 0;
>> }
>> 
>> Just curious.
>> 
>
>The key difference is that in C, the results of the conditional operator 
>is always an r-value, and thus not suitable for an assignment operator.
>
>By using the address-of and deference operatiors you convert that 
>r-value into an l-value so can make the assignment.
>
>In C++, because it has a "reference" type, if the last two terms of the 
>conditional operator are objects of a compatible type, then the type of 
>the result can be a reference to that type, an option not available in 
>C, and this allows its value to be an l-value.

Ok, that makes sense. Though I wonder - given the main C compilers used now
are also C++ compilers - why they don't just silently allow it in C? Would
there be any wierd or unfortunate side effects in C if it was?

[toc] | [prev] | [next] | [standalone]


#169184

FromRichard Damon <Richard@Damon-Family.org>
Date2023-02-04 13:02 -0500
Message-ID<Y4xDL.121911$Ldj8.36273@fx47.iad>
In reply to#169183
On 2/4/23 11:59 AM, Muttley@dastardlyhq.com wrote:
> On Sat, 4 Feb 2023 11:04:12 -0500
> Richard Damon <Richard@Damon-Family.org> wrote:
>> On 2/4/23 10:30 AM, Muttley@dastardlyhq.com wrote:
>>> Can anyone explain the "official" explanation from whichever standard as to
>>> why the first assignment only compiles in C++ but not C:
>>>
>>> #include <stdio.h>
>>>
>>> int main()
>>> {
>>>           int a = 0;
>>>           int b = 0;
>>>           int x = 0;
>>>           int y = 1;
>>>           int c = 123;
>>>
>>>           /* Compiles in C++, not C
>>>           (x == y ? a : b) = c;
>>>           */
>>>
>>>           /* C version */
>>>           *(x == y ? &a : &b) = c;
>>>
>>>           printf("a = %d, b = %d\n",a,b);
>>>           return 0;
>>> }
>>>
>>> Just curious.
>>>
>>
>> The key difference is that in C, the results of the conditional operator
>> is always an r-value, and thus not suitable for an assignment operator.
>>
>> By using the address-of and deference operatiors you convert that
>> r-value into an l-value so can make the assignment.
>>
>> In C++, because it has a "reference" type, if the last two terms of the
>> conditional operator are objects of a compatible type, then the type of
>> the result can be a reference to that type, an option not available in
>> C, and this allows its value to be an l-value.
> 
> Ok, that makes sense. Though I wonder - given the main C compilers used now
> are also C++ compilers - why they don't just silently allow it in C? Would
> there be any wierd or unfortunate side effects in C if it was?
> 

Mostly because in C it is a Required Diagnostic, so to allow it they 
need to at least issue a warning, so it can't be "silent" without 
something to at least implicitly define you don't want conformance.

Also, basically it goes down to the differing type inference rules of 
the two langauges, so it would need some explicit code injected into the 
C parsing part of the compiler.

While I can't think of any specific "harm" that allowing it in C as an 
extention would cause, since it is always a full error in C, I;m not 
sure that adding it would add that much value as an extenuation since it 
makes the code needlessly unportable, since the C version isn't that 
more complicated.

[toc] | [prev] | [next] | [standalone]


#169186

FromKeith Thompson <Keith.S.Thompson+u@gmail.com>
Date2023-02-04 12:45 -0800
Message-ID<87y1pdupkz.fsf@nosuchdomain.example.com>
In reply to#169183
Muttley@dastardlyhq.com writes:
[...]
> Ok, that makes sense. Though I wonder - given the main C compilers used now
> are also C++ compilers - why they don't just silently allow it in C? Would
> there be any wierd or unfortunate side effects in C if it was?

No, most C compilers are not also C++ compilers.

gcc and g++, for example, are part of the same project, but the front
ends (where conditional expressions are handled) are separate.

For example, I've never seen a grammar that conditionally treats "class"
as a keyword depending on the language being processed, and I'd be
surprised to see a compiler that uses that approach.

A C compiler could support treating conditional expressions as lvalues
as an extension (which would be non-conforming if it didn't produce a
diagnostic message), and it might be implemented by borrowing code from
the corresponding C++ front end, but I wouldn't expect the same code to
handle conditional expressions in C and C++ compilers.

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

[toc] | [prev] | [next] | [standalone]


#169188

FromDavid Brown <david.brown@hesbynett.no>
Date2023-02-05 11:42 +0100
Message-ID<tro15s$2f7lc$1@dont-email.me>
In reply to#169183
On 04/02/2023 17:59, Muttley@dastardlyhq.com wrote:

> Ok, that makes sense. Though I wonder - given the main C compilers used now
> are also C++ compilers - why they don't just silently allow it in C? Would
> there be any wierd or unfortunate side effects in C if it was?
> 

Most C++ compilers have a closely related C compiler that is part of the 
same overall compiler project, but the front ends for C and C++ are 
usually separate programs.  So major C++ compilers such as gcc, clang, 
MSVC, ICC all support C within the same overall toolchain.  But it is 
normal that the front-end parsers are separate, even though the 
middle-end optimisers and back-end code generators are shared.

On the other hand, the vast majority of C compilers are /not/ C++ 
compilers.  With a bit of googling, you'll probably find a couple of 
dozen C compilers available for PC's (Linux and/or Windows) freely 
available, and only a few of them will also have C++ compilers.  Go over 
to the embedded world and you can find hundreds of C compilers, and only 
perhaps 5% of them support C++.

If you look at hobby projects, one-man toolchains and niche compilers, I 
have never heard of any that support C++, while a working C compiler is 
something many people have written.

[toc] | [prev] | [next] | [standalone]


#169198

FromMuttley@dastardlyhq.com
Date2023-02-06 09:26 +0000
Message-ID<trqh4c$31588$1@dont-email.me>
In reply to#169188
On Sun, 5 Feb 2023 11:42:04 +0100
David Brown <david.brown@hesbynett.no> wrote:
>On the other hand, the vast majority of C compilers are /not/ C++ 

Ok, I should have said most of the *common* C++ compilers are also C compilers.

>If you look at hobby projects, one-man toolchains and niche compilers, I 
>have never heard of any that support C++, while a working C compiler is 
>something many people have written.

I don't know if its true or not, but I read once that lexx and yacc are
quite capable of parsing C grammar and create the appropriate lex and parse
code but the C++ syntax is apparently beyond them because there are so many
token overloads and special cases. And that was IIRC before C++ 2011, never
mind now.

[toc] | [prev] | [next] | [standalone]


#169201

FromDavid Brown <david.brown@hesbynett.no>
Date2023-02-06 14:13 +0100
Message-ID<trqueg$33cks$1@dont-email.me>
In reply to#169198
On 06/02/2023 10:26, Muttley@dastardlyhq.com wrote:
> On Sun, 5 Feb 2023 11:42:04 +0100
> David Brown <david.brown@hesbynett.no> wrote:
>> On the other hand, the vast majority of C compilers are /not/ C++
> 
> Ok, I should have said most of the *common* C++ compilers are also C compilers.
> 

No, that would still be wrong.  You should have said - as I wrote - that 
most C++ compilers have a C compiler as part of their toolchain packages.

I don't know of any C++ compiler, common or not, that does not have an 
associated C compiler.  (The nearest, perhaps, would be MSVC - there you 
have a modern C++ compiler combined with an outdated and non-conforming 
C compiler.)

[toc] | [prev] | [next] | [standalone]


#169202

FromMuttley@dastardlyhq.com
Date2023-02-06 15:50 +0000
Message-ID<trr7ku$3559m$1@dont-email.me>
In reply to#169201
On Mon, 6 Feb 2023 14:13:51 +0100
David Brown <david.brown@hesbynett.no> wrote:
>On 06/02/2023 10:26, Muttley@dastardlyhq.com wrote:
>> On Sun, 5 Feb 2023 11:42:04 +0100
>> David Brown <david.brown@hesbynett.no> wrote:
>>> On the other hand, the vast majority of C compilers are /not/ C++
>> 
>> Ok, I should have said most of the *common* C++ compilers are also C
>compilers.
>> 
>
>No, that would still be wrong.  You should have said - as I wrote - that 
>most C++ compilers have a C compiler as part of their toolchain packages.

You're splitting hairs. "Compiler" for most people is the entire toolchain,
not one particular part of it.

[toc] | [prev] | [next] | [standalone]


#169204

FromDavid Brown <david.brown@hesbynett.no>
Date2023-02-06 20:08 +0100
Message-ID<trrj80$35q8u$1@dont-email.me>
In reply to#169202
On 06/02/2023 16:50, Muttley@dastardlyhq.com wrote:
> On Mon, 6 Feb 2023 14:13:51 +0100
> David Brown <david.brown@hesbynett.no> wrote:
>> On 06/02/2023 10:26, Muttley@dastardlyhq.com wrote:
>>> On Sun, 5 Feb 2023 11:42:04 +0100
>>> David Brown <david.brown@hesbynett.no> wrote:
>>>> On the other hand, the vast majority of C compilers are /not/ C++
>>>
>>> Ok, I should have said most of the *common* C++ compilers are also C
>> compilers.
>>>
>>
>> No, that would still be wrong.  You should have said - as I wrote - that
>> most C++ compilers have a C compiler as part of their toolchain packages.
> 
> You're splitting hairs. "Compiler" for most people is the entire toolchain,
> not one particular part of it.
> 

Those hairs are critical when you are considering "Why doesn't C++ 
support this if C does?" or "Why doesn't C support this if C++ does?".

I agree that for most people in normal use, the details of how 
toolchains work is unimportant.  You use the command "gcc" or "clang", 
and it will compile C and C++, do the linking, handle assembly, and 
often handle Objective-C, Fortran, and maybe other languages.  But we 
can be a bit more advanced in these groups.  You wanted to know whether 
it would be simple for a C compiler to support a particular feature that 
is standard in C++ - one of the reasons why it is not immediately easy 
to add as an extension to C is that the compilers have independent 
frond-ends.

[toc] | [prev] | [next] | [standalone]


#169205

FromKeith Thompson <Keith.S.Thompson+u@gmail.com>
Date2023-02-06 12:47 -0800
Message-ID<87zg9qed24.fsf@nosuchdomain.example.com>
In reply to#169202
Muttley@dastardlyhq.com writes:
> On Mon, 6 Feb 2023 14:13:51 +0100
> David Brown <david.brown@hesbynett.no> wrote:
>>On 06/02/2023 10:26, Muttley@dastardlyhq.com wrote:
>>> On Sun, 5 Feb 2023 11:42:04 +0100
>>> David Brown <david.brown@hesbynett.no> wrote:
>>>> On the other hand, the vast majority of C compilers are /not/ C++
>>> 
>>> Ok, I should have said most of the *common* C++ compilers are also C
>>compilers.
>>> 
>>
>>No, that would still be wrong.  You should have said - as I wrote - that 
>>most C++ compilers have a C compiler as part of their toolchain packages.
>
> You're splitting hairs. "Compiler" for most people is the entire toolchain,
> not one particular part of it.

Perhaps, but the reason you said that most C compilers are also C++
compilers (or however you phrased it) was to support the idea that
it would be trivial to implement rvalue conditional expressions in C.
The part of a C compiler that implements conditional expressions is,
in all cases I'm aware of, distinct from the part of a C++ compiler
that implements conditional expressions.  Possibly some code could
be borrowed, but it's not just a matter of turning off a restriction.

The "entire toolchain" is not relevant to the point you raised.

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

[toc] | [prev] | [next] | [standalone]


#169207

FromKeith Thompson <Keith.S.Thompson+u@gmail.com>
Date2023-02-06 13:03 -0800
Message-ID<87r0v2ecc0.fsf@nosuchdomain.example.com>
In reply to#169198
Muttley@dastardlyhq.com writes:
> On Sun, 5 Feb 2023 11:42:04 +0100
> David Brown <david.brown@hesbynett.no> wrote:
>>On the other hand, the vast majority of C compilers are /not/ C++ 
>
> Ok, I should have said most of the *common* C++ compilers are also C compilers.

Discussed elsewhere.

>>If you look at hobby projects, one-man toolchains and niche compilers, I 
>>have never heard of any that support C++, while a working C compiler is 
>>something many people have written.
>
> I don't know if its true or not, but I read once that lexx and yacc are
> quite capable of parsing C grammar and create the appropriate lex and parse
> code but the C++ syntax is apparently beyond them because there are so many
> token overloads and special cases. And that was IIRC before C++ 2011, never
> mind now.

C's grammar is not trivial to parse, due to typedefs.  A typedef
effectively creates a context-specific keyword, which means the parser
has to interact with the symbol table.  (Historically, this is because
typedefs were introduced relatively late in the evolution of C.)

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

[toc] | [prev] | [next] | [standalone]


#169215

FromMuttley@dastardlyhq.com
Date2023-02-07 09:24 +0000
Message-ID<trt5cs$3gvt8$1@dont-email.me>
In reply to#169207
On Mon, 06 Feb 2023 13:03:11 -0800
Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote:
>Muttley@dastardlyhq.com writes:
>> On Sun, 5 Feb 2023 11:42:04 +0100
>> David Brown <david.brown@hesbynett.no> wrote:
>>>On the other hand, the vast majority of C compilers are /not/ C++ 
>>
>> Ok, I should have said most of the *common* C++ compilers are also C
>compilers.
>
>Discussed elsewhere.
>
>>>If you look at hobby projects, one-man toolchains and niche compilers, I 
>>>have never heard of any that support C++, while a working C compiler is 
>>>something many people have written.
>>
>> I don't know if its true or not, but I read once that lexx and yacc are
>> quite capable of parsing C grammar and create the appropriate lex and parse
>> code but the C++ syntax is apparently beyond them because there are so many
>> token overloads and special cases. And that was IIRC before C++ 2011, never
>> mind now.
>
>C's grammar is not trivial to parse, due to typedefs.  A typedef
>effectively creates a context-specific keyword, which means the parser
>has to interact with the symbol table.  (Historically, this is because
>typedefs were introduced relatively late in the evolution of C.)

Could it not cheat and simply treat a typedef as a kind of macro?

[toc] | [prev] | [next] | [standalone]


#169220

FromKeith Thompson <Keith.S.Thompson+u@gmail.com>
Date2023-02-07 11:08 -0800
Message-ID<87edr1e1js.fsf@nosuchdomain.example.com>
In reply to#169215
Muttley@dastardlyhq.com writes:
> On Mon, 06 Feb 2023 13:03:11 -0800
> Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote:
[...]
>>C's grammar is not trivial to parse, due to typedefs.  A typedef
>>effectively creates a context-specific keyword, which means the parser
>>has to interact with the symbol table.  (Historically, this is because
>>typedefs were introduced relatively late in the evolution of C.)
>
> Could it not cheat and simply treat a typedef as a kind of macro?

No, typedefs have scope, and a declaration in an inner scope can hide a
typedef in an outer scope.

#include <stdio.h>
int main(void) {
    typedef int Integer;
    {
        int Integer = 10;
        printf("Integer = %d\n", Integer);
    }
    // int Integer = 20; // would be a syntax error
}

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

[toc] | [prev] | [next] | [standalone]


#169222

FromMuttley@dastardlyhq.com
Date2023-02-08 09:23 +0000
Message-ID<trvpmu$48m5$1@dont-email.me>
In reply to#169220
On Tue, 07 Feb 2023 11:08:23 -0800
Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote:
>Muttley@dastardlyhq.com writes:
>> On Mon, 06 Feb 2023 13:03:11 -0800
>> Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote:
>[...]
>>>C's grammar is not trivial to parse, due to typedefs.  A typedef
>>>effectively creates a context-specific keyword, which means the parser
>>>has to interact with the symbol table.  (Historically, this is because
>>>typedefs were introduced relatively late in the evolution of C.)
>>
>> Could it not cheat and simply treat a typedef as a kind of macro?
>
>No, typedefs have scope, and a declaration in an inner scope can hide a
>typedef in an outer scope.
>
>#include <stdio.h>
>int main(void) {
>    typedef int Integer;
>    {
>        int Integer = 10;
>        printf("Integer = %d\n", Integer);
>    }
>    // int Integer = 20; // would be a syntax error
>}

Do you know I never knew you could do that with C. I thought it was C++ only.
Learnt something new today!

[toc] | [prev] | [next] | [standalone]


#169228

FromBlue-Maned_Hawk <bluemanedhawk@gmail.com>
Date2023-02-08 22:13 -0500
Message-ID<ts1ocr$hffi$1@bluemanedhawk.eternal-september.org>
In reply to#169220
On 2/7/23 14:08, Keith Thompson wrote:
> Muttley@dastardlyhq.com writes:
>> On Mon, 06 Feb 2023 13:03:11 -0800
>> Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote:
> [...]
>>> C's grammar is not trivial to parse, due to typedefs.  A typedef
>>> effectively creates a context-specific keyword, which means the parser
>>> has to interact with the symbol table.  (Historically, this is because
>>> typedefs were introduced relatively late in the evolution of C.)
>>
>> Could it not cheat and simply treat a typedef as a kind of macro?
> 
> No, typedefs have scope, and a declaration in an inner scope can hide a
> typedef in an outer scope.
> 

​So could it treat it as a macro that gets `#undef`ed at the end of a scope?

-- 
⚗︎ | /blu.mɛin.dʰak/ | shortens to "Hawk" | he/him/his/himself/Mr.
bluemanedhawk.github.io
Bitches stole my whole ass ␔🭖᷿᪳𝼗᷍⏧𒒫𐻾ࣛ↉�⃣ quoted-printable, can't 
have shit in Thunderbird 😩

[toc] | [prev] | [next] | [standalone]


#169229

FromJames Kuyper <jameskuyper@alumni.caltech.edu>
Date2023-02-08 22:53 -0500
Message-ID<ts1qo8$hoct$1@dont-email.me>
In reply to#169228
On 2/8/23 22:13, Blue-Maned_Hawk wrote:
> On 2/7/23 14:08, Keith Thompson wrote:
>> Muttley@dastardlyhq.com writes:
>>> On Mon, 06 Feb 2023 13:03:11 -0800
>>> Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote:
>> [...]
>>>> C's grammar is not trivial to parse, due to typedefs. A typedef
>>>> effectively creates a context-specific keyword, which means the parser
>>>> has to interact with the symbol table. (Historically, this is because
>>>> typedefs were introduced relatively late in the evolution of C.)
>>>
>>> Could it not cheat and simply treat a typedef as a kind of macro?
>>
>> No, typedefs have scope, and a declaration in an inner scope can hide a
>> typedef in an outer scope.
>>
>
> ​So could it treat it as a macro that gets `#undef`ed at the end of a
> scope?

The same identifier can be two different typedefs in two different
nested scopes. The typedef in the inner scope hides the typedef in the
outer scope, but the typedef in the outer scope becomes visible again at
the end of the inner scope. Therefore, you'd have to have an #undef at
the end of the inner scope immediately followed by a #define.

Note: macro replacement occurs during translation phase 4. Scopes don't
even exist until translation phase 7, and can be affected by some of the
transformations that can occur during translation phase 4. That is to
say, conditional compilation, #include directives or the expansion of a
macro could create or eliminate scopes that would not have been
identified as such in the code as it existed prior to translation phase 4.

Basically, to do something similar to what you're suggesting, you need
to first perform all phase 4, 5, and 6 transformations properly, then
start translation phase 7 parsing to identify where scopes start and
end. Then you have to insert #define and #undef directives in the
appropriate places to implement typedefs, and carry out a
mini-translation phase 4 limited to recognizing those #define and #undef
directives. I don't think that this late phase 4 processing would have
any significant advantage over just biting the bullet and implementing
typedefs correctly during translation phase 7.

[toc] | [prev] | [next] | [standalone]


#169230

FromKeith Thompson <Keith.S.Thompson+u@gmail.com>
Date2023-02-08 22:12 -0800
Message-ID<87wn4rcqp5.fsf@nosuchdomain.example.com>
In reply to#169228
Blue-Maned_Hawk <bluemanedhawk@gmail.com> writes:
> On 2/7/23 14:08, Keith Thompson wrote:
>> Muttley@dastardlyhq.com writes:
>>> On Mon, 06 Feb 2023 13:03:11 -0800
>>> Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote:
>> [...]
>>>> C's grammar is not trivial to parse, due to typedefs.  A typedef
>>>> effectively creates a context-specific keyword, which means the parser
>>>> has to interact with the symbol table.  (Historically, this is because
>>>> typedefs were introduced relatively late in the evolution of C.)
>>>
>>> Could it not cheat and simply treat a typedef as a kind of macro?
>> No, typedefs have scope, and a declaration in an inner scope can
>> hide a
>> typedef in an outer scope.
>
> ​So could it treat it as a macro that gets `#undef`ed at the end of a scope?

No, that wouldn't even work for the example I posted.  The "macro"
wouldn't be undefined until the end of main.

#include <stdio.h>
int main(void) {
    typedef int Integer;
    {
        int Integer = 10;
        printf("Integer = %d\n", Integer);
    }
    // int Integer = 20; // would be a syntax error
}

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

[toc] | [prev] | [next] | [standalone]


#169221

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2023-02-08 02:52 +0000
Message-ID<87v8kclvgu.fsf@bsb.me.uk>
In reply to#169215
Muttley@dastardlyhq.com writes:

> On Mon, 06 Feb 2023 13:03:11 -0800
> Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote:
<cut>
>>C's grammar is not trivial to parse, due to typedefs.  A typedef
>>effectively creates a context-specific keyword, which means the parser
>>has to interact with the symbol table.  (Historically, this is because
>>typedefs were introduced relatively late in the evolution of C.)
>
> Could it not cheat and simply treat a typedef as a kind of macro?

Keith has mentioned the scope issue, but there are other problems with a
simple textual substitution.  For example, after

  typedef int *T;

the declaration const T x; gives x a const-qualified type (i.e. you
can't assign to x).  A simple substitution of "T" for "int *" does not
have the same effect at all.

-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#169196

FromPaavo Helde <eesnimi@osa.pri.ee>
Date2023-02-06 09:11 +0200
Message-ID<trq970$2vkp9$1@dont-email.me>
In reply to#169183
04.02.2023 18:59 Muttley@dastardlyhq.com kirjutas:
> On Sat, 4 Feb 2023 11:04:12 -0500
> Richard Damon <Richard@Damon-Family.org> wrote:
>> On 2/4/23 10:30 AM, Muttley@dastardlyhq.com wrote:
>>> Can anyone explain the "official" explanation from whichever standard as to
>>> why the first assignment only compiles in C++ but not C:
>>>
>>> #include <stdio.h>
>>>
>>> int main()
>>> {
>>>           int a = 0;
>>>           int b = 0;
>>>           int x = 0;
>>>           int y = 1;
>>>           int c = 123;
>>>
>>>           /* Compiles in C++, not C
>>>           (x == y ? a : b) = c;
>>>           */
>>>
>>>           /* C version */
>>>           *(x == y ? &a : &b) = c;
>>>
>>>           printf("a = %d, b = %d\n",a,b);
>>>           return 0;
>>> }
>>>
>>> Just curious.
>>>
>>
>> The key difference is that in C, the results of the conditional operator
>> is always an r-value, and thus not suitable for an assignment operator.
>>
>> By using the address-of and deference operatiors you convert that
>> r-value into an l-value so can make the assignment.
>>
>> In C++, because it has a "reference" type, if the last two terms of the
>> conditional operator are objects of a compatible type, then the type of
>> the result can be a reference to that type, an option not available in
>> C, and this allows its value to be an l-value.
> 
> Ok, that makes sense. Though I wonder - given the main C compilers used now
> are also C++ compilers - why they don't just silently allow it in C? Would
> there be any wierd or unfortunate side effects in C if it was?

This would basically introduce a limited notion of 'reference' in the C 
language, which would make the language more complex to teach and learn. 
That's the opposite to the goals C currently has.

And yes, having a simpler language in general means the programs written 
in it need to be more complex. Here it just means a couple of extra 
asterisks and ampersands, so not a big deal.

[toc] | [prev] | [next] | [standalone]


Page 1 of 2  [1] 2  Next page →

Back to top | Article view | comp.lang.c


csiph-web