On 14/05/2012 5:23 PM, jacob navia wrote:
> Le 14/05/12 22:30, Wojtek Lerch a écrit :
...
>> Would you be against allowing this new feature to be applied to all
>> structure members regardless of their type, rather than just function
>> pointers?
>
> My main problem is inlining function calls to function pointers
> within structures. It could have other uses for immediate data,
> but in the context of C those uses are not so crucial, since
> if you want always the integer 2 to be at the first position of
> the structure you can initialize it to that without any real
> problem. Inline calls are different because they NEED to be
> known to the compiler.
Still, if the compiler *knew* that it's 2 in all instances of the
structure, it could "inline" it in the sense that it would become a
compile-time constant, sometimes allowing the compiler to completely
throw away whole branches of conditionals, etc.
>> For instance, do you think the same new syntax could be used
>> to specify the values of SomeData and MoreData from your example?
>>
>
> That would be a more general solution, yes. I tend to try to
> ask the smallest solution possible to avoid getting people angry
> at me.
I can think of a few ways you could try that might work better... In
this case, I think your minimalism hurts your proposal, because it
reduces its potential benefits without really reducing the complexity or
the size of the language change.
>> char *Interface.SomeData = "Hello";
>> double complex Interface.MoreData = I;
>>
>> If your goal is to enforce the struct member to be initialized to a
>> particular value so that the compiler knows it without reading it from
>> memory, is there a point in keeping it in memory anyway? If the
>> declarations make it obvious to the compiler that the Deg2Rad member of
>> any Interface object points to the same function, why would I write code
>> that uses the pointer inside the structure to call the function, instead
>> of just calling the function directly by name?
>
> Obviously because you do NOT want ALL function pointers in a structure
> inlined!
Excuse me? Are you saying that I would want to use the pointer in the
structure instead of directly calling Common_Deg2Rad() in those cases
when I do *not* want the call inlined? And that that's obvious???
Wasn't the whole purpose of your proposal to ensure that an indirect
call through the pointer could get inlined just like a direct call would?
Or did you just misunderstand my question? Let me try again:
Given your struct definition and my
inline int Common_Deg2Rad(double foo) { return foo/57.295; }
int (*Interface1.Deg2Rad)(double foo) = Common_Deg2Rad;
what reasons would one have to use the expression
z = p->Deg2Rad( x );
instead of
z = Common_Deg2Rad( x );
It wouldn't be because I don't want the second one inlined, would it?
> Normally you have in an interface some functions that are just
> covers (like macros). For instance I could have defined
>
> #define Deg2rad(arg) (arg/52.295)
>
> but an inline function gets better type checking as everyone knows.
>
> So, it is better to use inline functions. Now, why put them in an
> interface?
First of all, what is an interface? And before I can think of ways of
putting inline functions in it, how do you put macros in it? Or regular
functions?
> 1) Documentation and code structuring.
> 2) Avoiding name pollution: the function is not part of the
> global namespace (the only one in C).
>
> There was a proposal to incorporate C++ syntax to define this:
>
> typedef struct interface{
> double (*Deg2Rad)(double arg) { return arg/57.295;}
> double foo;
> } Interface;
This is not valid C++, is it? Did you mean
typedef struct interface{
double Deg2Rad(double arg) { return arg/57.295;}
double foo;
} Interface;
If so, then this is completely different from what I thought you wanted
to propose. In this C++ example, "Deg2Rad" is not a pointer sitting in
the structure. It's just a regular function with a funny name
(interface::Deg2Rad) and an implicit extra parameter (struct interface
*this) that needs to be passed to the function using a funny syntax that
looks like the syntax of an indirect call through a pointer sitting
inside the structure. The reason the compiler knows that a call such as
p->Deg2Rad(45) calls that particular function, and that it can therefore
inline it, is not because it knows what function the pointer member in
the structure points to -- it's because there no pointer there, and the
expression directly names the function it calls.
> Problem is:
>
> Are the OTHER members of the structure that is not yet fully
> defined available ? I do not know the answer in the context
> of C, and I wouldn't want to add a construct that REQUIRES a two
> pass compiler into the language. lcc-win is a one pass
> compiler, for instance, and it is very fast.
>
> The syntax
>
> double Interface.Deg2Rad(double arg) {return arg/57.295;}
>
> has the advantage of avoiding those questions and keeping the
> compiler one pass.
I suspect this syntax would also have many disadvantages, but I would
have to see a better description of the semantics first. The devil is
in the detail.
> But it could be OK to add the C++ syntax if we decide that
> struct members further DOWN the definition are just not available!
> That would avoid requiring two passes. But then, we would have
> the same syntax as C++ with quite different semantics, what would
> build a big gotcha! for C++ people that use this.
No argument there. Identical syntax with different semantics would be a
disaster.
> A completely different syntax is better than having all those problems
> in my opinion.
Agreed. A detailed description of the semantics that you want the new
syntax to have would be helpful though. A description that is too
detailed and explains obvious things is not going to hurt your case as
much as an example of the syntax and a vague comment implying that the
semantics that you have in mind are obvious. Obviously, they're obvious
to you; but the rest of us prefer to read a written explanation instead
of trying to read your mind.