I think most people is not aware of this:
From 3096 C23 draft
"
6.7.9 Type inference
...
2 For such a declaration that is the definition of an object the
init- declarator shall have the form
direct-declarator = assignment-expression
"
Basically "direct-declarator" differs from "declarator" because it
does not contains pointer.
Then the type inference using auto and pointer is something undefined
in C23.
struct node{
struct node * next;
};
int main(){
struct node node = {};
auto * p = node.next;
}
<source>:7:4: error: 'auto' requires a plain identifier, possibly
with attributes, as declarator
7 | auto * p = node.next;
| ^~~~
This differs from C++.
Am 26.05.2024 um 15:49 schrieb Thiago Adams:
I think most people is not aware of this:
From 3096 C23 draft
"
6.7.9 Type inference
...
2 For such a declaration that is the definition of an object the
init- declarator shall have the form
direct-declarator = assignment-expression
"
Basically "direct-declarator" differs from "declarator" because it
does not contains pointer.
Then the type inference using auto and pointer is something undefined
in C23.
struct node{
struct node * next;
};
int main(){
struct node node = {};
auto * p = node.next;
}
<source>:7:4: error: 'auto' requires a plain identifier, possibly with
attributes, as declarator
7 | auto * p = node.next;
| ^~~~
This differs from C++.
I don't know what type inference in C is good for since the type names
in C are usually short. If I have short typenames in C++ I don't use
type inference. Type-inference makes sense to make such things shorter
typename map<string, string>::const_iterator it = map.cbegin();
This doesn't happen in C.
I don't know what type inference in C is good for since the type names
in C are usually short. If I have short typenames in C++ I don't use
type inference. Type-inference makes sense to make such things shorter
typename map<string, string>::const_iterator it = map.cbegin();
This doesn't happen in C.
It would have been nice to see statement expressions included in C23, as
they have been in gcc for ages:
#define max(a,b) \
({ __auto_type _a = (a); \
__auto_type _b = (b); \
_a > _b ? _a : _b; })
David Brown wrote:
It would have been nice to see statement expressions included in C23, as
they have been in gcc for ages:
#define max(a,b) \
({ __auto_type _a = (a); \
__auto_type _b = (b); \
_a > _b ? _a : _b; })
As i understand it, WG14's plan is to eventually get lambdas into standard
C so as to unify statementexprs with Clang's Blocks and GCC's local subroutines.
Em 5/26/2024 1:10 PM, David Brown escreveu:
On 26/05/2024 16:22, Bonita Montero wrote:
Am 26.05.2024 um 15:49 schrieb Thiago Adams:
I think most people is not aware of this:
From 3096 C23 draft
"
6.7.9 Type inference
...
2 For such a declaration that is the definition of an object the
init- declarator shall have the form
direct-declarator = assignment-expression
"
Basically "direct-declarator" differs from "declarator" because it
does not contains pointer.
Then the type inference using auto and pointer is something
undefined in C23.
struct node{
struct node * next;
};
int main(){
struct node node = {};
auto * p = node.next;
}
<source>:7:4: error: 'auto' requires a plain identifier, possibly
with attributes, as declarator
7 | auto * p = node.next;
| ^~~~
This differs from C++.
I don't know what type inference in C is good for since the type names
in C are usually short. If I have short typenames in C++ I don't use
type inference. Type-inference makes sense to make such things shorter
typename map<string, string>::const_iterator it = map.cbegin(); >>> This doesn't happen in C.
"typeof" and "auto" have been available forever as gcc extensions
(where "auto" was spelt "__auto_type", since of course "auto" had
another meaning in C until C23). One use-case for C is in macros that
handle multiple types, but I expect people have done other things with
them too.
It would have been nice to see statement expressions included in C23,
as they have been in gcc for ages:
#define max(a,b) \
({ __auto_type _a = (a); \
__auto_type _b = (b); \
_a > _b ? _a : _b; })
In general, it's just another tool that could be useful in writing
code that's a bit more flexible.
I am trying to remember the situation where typeof cannot be used,
justifying the existence of auto other than "easy to read".
#define max(a,b) \
({ typeof(a) _a = (a); \
typeof(a) _b = (b); \
_a > _b ? _a : _b; })
I think for function calls typeof can be a little confusing because the arguments.
typeof(f(arg1, arg2)) r = f(arg1, arg2);
auto r = f(arg1, arg2);
I am trying to remember the situation where typeof cannot be used,
justifying the existence of auto other than "easy to read".
But "blocks" (borrowed from Objective-C, IIRC) are more advanced than
gcc's statement expressions, so I suppose lambdas are a reasonable
superset of these three extensions.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 546 |
Nodes: | 16 (2 / 14) |
Uptime: | 150:47:33 |
Calls: | 10,383 |
Files: | 14,054 |
Messages: | 6,417,791 |