D
Default User
Richard said:[csc snipped]
Default User said:
We like to call everybody by their first name. Do you prefer Ricky
or Richie?
No.
Ok, Dickie it is.
Brian
Richard said:[csc snipped]
Default User said:
We like to call everybody by their first name. Do you prefer Ricky
or Richie?
No.
We like to call everybody by their first name. Do you prefer Ricky or
Richie?
Ian said:struct HoldSomething
{
Something held;
HoldSomething() { held = AcquireSomething(); }
~HoldSomething() { Release Something( held ); }
};
Something can be any resource to be held by the scope. It is released
when the HoldSomething object goes out of scope.
[...]jacob navia said:One example is the "addition" of strings instead of concatenation as
a function call. When A and B are strings, A+B != B+A, what is not at
all clear to me. It is better to use a function call to do that
operation.
NO, you can not. HoldSomething's destructor is called when the objectjacob said:Fine. But why can't you write exactly the same code in
your new_foo() function???
You can do ALL that in new_foo(). The *only* difference is that is NOT
called automatically!
The same for the destructor destroy_foo(). You can release the resource
JUST AS you do in your C++ destructor!
A very important difference.The only differences are some syntax and the fact that the
compiler will not call it for you!
Keith said:[...]jacob navia said:One example is the "addition" of strings instead of concatenation as
a function call. When A and B are strings, A+B != B+A, what is not at
all clear to me. It is better to use a function call to do that
operation.
That's a matter of taste.
Ian Collins said:jacob navia wrote: [...]Maybe you should read up on RAII.What is wrong with
FOO myFoo = new_foo(67,"J. Smith");
I just do not see the difference to C++.
Why is this constructor worst than its C++ counterpart?
A common C++ idiom is
struct HoldSomething
{
Something* held;
HoldSomething( Something* s ) { held = s; }
~HoldSomething() { delete held; }
};
or
struct HoldSomething
{
Something held;
HoldSomething() { held = AcquireSomething(); }
~HoldSomething() { Release Something( held ); }
};
Something can be any resource to be held by the scope. It is released
when the HoldSomething object goes out of scope.
Keith said:In other words, having to invoke the constructor explicitly isn't a
huge deal, since you're declaring the object anyway, but having the
destructor invoked for you when the object ceases to exist is a very
nice convenience. jacob has argued against C++-style destructors
because they're difficult to implement; the point is to make the
compiler, rather than the programmer, do that hard work.
Richard said:[csc snipped]
Default User said:
We like to call everybody by their first name. Do you prefer Ricky
or Richie?
No.
Ok, Dickie it is.
Richard said:Default User said:
You might want to reconsider that.
[snip]C + constructors/destructors + templates = C++ - overloading -
exceptions - inheritance/methods.
If we combine this proposal with jacob navia proposal, we get:
Which doesn't mean that C++ would have been small. I don't
think that the biggest part of C++ isn't in inheritance and
methods. Actually, I would rather think that every component of
C++ participates to the size of the language.
Maybe I'm wrong, and templates + constructors/destructors
are very easy to implement in a few LOC, but you've to give
evidence in order to prove that your proposal is realistic.
I don't know how many lines of code it would be, but template
implementations already exist for C++ compilers. It seems that
C compiler vendors would not be overly burdened to provide the
same feature for C.
You're wandering of into rant land again... The topic was the automaticjacob said:This is the way C++ went. It means that every imaginable feature was
added to the language, in a baroque construction that defies
gravity.
There I would agree with you, don't forget I wasn't proposing addingNo, it is not that I fear implementing constructors, but that I
fear that that feature will make the language more opaque, in the
sense of too much IMPLICIT code.
That's pretty obvious.And that feature needs other features:
struct foo {
int a;
struct foo1 f1;
int b;
};
When we destroy foo, we should call the destructor of foo1
first, isn't it?
Pointers don't have destructors.And what happens with:
struct foo {
int a;
struct foo1 *pf1;
};
Should we call the destructor of f1 with the pf1 pointer?
Or only when pf1 is not NULL?
C++ rules for destructor sequencing are pretty simple and logical.This will produce the same complex set of rules that
C++ has.
It probably wouldn't be that bad. C++ treats function templates asjacob said:jxh wrote:
[snip]
I don't know how many lines of code it would be, but template
implementations already exist for C++ compilers. It seems that
C compiler vendors would not be overly burdened to provide the
same feature for C.
You are dreaming.
Template implementations are C++ implementations of templates, that
suppose a C++ object layout, a class hierarchy, traits and many other
features that are absent in C. The compiler people would need to
take out those "features" of C++ from their template implementations
and in those complex mountains of code that would be a work for titans.
Ian said:You're wandering of into rant land again... The topic was the automatic
calling of destructors.
There I would agree with you, don't forget I wasn't proposing adding
them, just putting forward an example of where they can be useful and
enable an idiom not supported by C.
That's pretty obvious.
Pointers don't have destructors.
C++ rules for destructor sequencing are pretty simple and logical.
Didn't you see the bit above where I said I agreed with you?Same as above. Pretty obvious but why not? Does this means
that destructors can't be called ever with the result of malloc?
Ahh but wait... we should introduce "new" then, to do the
allocaton that will need cleanup.
You see the problems?
I paid my $18 and downloaded it the day it was released. Not lightHave you tried to read the C++ standard? I did.
jacob navia said:Keith said:[...]jacob navia said:One example is the "addition" of strings instead of concatenation as
a function call. When A and B are strings, A+B != B+A, what is not at
all clear to me. It is better to use a function call to do that
operation.
That's a matter of taste.
True. The operation "Concatenate this strings" is in
my mind quite different from "Addition" , but maybe is
just a personal problem.
More serious misuses of operator overloading are when you try to give
meaning to meaningless operations like adding two dates for instance.
In general mathematical operations should be reserved for
*numbers*. This is a rule I have tried to follow, but
I can imagine that many people will use + to concatenate strings
when is available.
"2"+"2" --> "22"
You are dreaming.
Template implementations are C++ implementations of templates,
that suppose a C++ object layout, a class hierarchy, traits
and many other features that are absent in C. The compiler
people would need to take out those "features" of C++ from
their template implementations and in those complex mountains
of code that would be a work for titans.
jacob navia said:This is the way C++ went. It means that every imaginable feature was
added to the language, in a baroque construction that defies
gravity.
No, it is not that I fear implementing constructors, but that I
fear that that feature will make the language more opaque, in the
sense of too much IMPLICIT code.
And that feature needs other features:
struct foo {
int a;
struct foo1 f1;
int b;
};
When we destroy foo, we should call the destructor of foo1
first, isn't it?
And what happens with:
struct foo {
int a;
struct foo1 *pf1;
};
Should we call the destructor of f1 with the pf1 pointer?
Or only when pf1 is not NULL?
This will produce the same complex set of rules that
C++ has.
Look. Let's NOT redo C++. C is simple. If you want
"the whole enchilada"... then program in C++ of course.
C and C++ remain distinct languages. C keeps things simple
and is fast, C++ has other uses, and can be as fast as C
sometimes.
Isn't that just function overloading with a different name?
Any form of templates requires some form of function name
overloading and name mangling.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.