POV-Ray : Newsgroups : povray.off-topic : Standard libraries Server Time
26 Nov 2026 04:40:27 EST (-0500)
  Standard libraries (Message 59 to 108 of 108)  
<<< Previous 50 Messages Goto Initial 50 Messages
From: Warp
Subject: Re: Standard libraries
Date: 7 Mar 2009 15:17:20
Message: <49b2d650@news.povray.org>
Darren New <dne### [at] sanrrcom> wrote:
> Warp wrote:
> > Darren New <dne### [at] sanrrcom> wrote:
> >> Warp wrote:
> >>>     std::for_each(someList, [&total](int x) { total += x });
> > 
> >> And, really, what's the advantage of this over a normal for loop?
> > 
> >   Nothing. Since it's C++ code, it sucks.

> You know, if you're not mature enough to have a civil conversation, you 
> don't really need to participate.

  You really are a master of trolling, you know. I have to admire that.

  First you succeed in trolling me with your unusually heavy and biased
C++ bashing, and then you try to make it look like all you wanted to do
is have a simple civil conversation.

  You know perfectly that there's at least one person reading this
newsgroup who appreciates the C++ language and doesn't appreciate
excessive bashing, yet you deliberately write really inflammatory
comments and mocking about it. Every question you made about new C++
features were aimed solely at trying to find flaws in them. When your
questions were answered, you asked more questions, trying to find flaws.
You continued asking questions until you found some flaw you could point
out. (Eg. first you asked if lambda functions would use such a cumbersome
syntax as boost::lambda, and when you got shown that they won't, you asked
if you can assign lamdbda functions to variables, and when you got shown
that you can, you continued asking and asking until you found something
that you can consider a flaw so that you could mock the whole feature.
The whole purpose of this was clearly to find some flaw you could point
out.)

  You know perfectly that if you start such a heavy bashing, you are going
to piss off at least one C++ fan, yet you deliberately go on, perfectly
knowing what effect it will cause. I think that deep inside you enjoy
doing that.

  Then, when you succeed in your trolling, you act all innocent and civil
and complain that all you wanted to do is have a "civil conversation".
After making me all angry with your condescending attitude you can go
and safely call me "immature" and make yourself look so high and mighty.

  You really are good at trolling. Personally I could not get even near
that level of mastery.

> I asked an actual useful question, so I 
> could perhaps learn why people are working to put lambdas into the language 
> when they have restrictions that make them useless for what most other 
> people use lambdas for. If your only answer is "there's no advantage of 
> lambdas over coding a regular for loop", then yeah, C++ sucks.

  The std::for_each() function was only a simple example. There are many
other algorithms which cannot be implemented with a simple loop.

  Another example: You could give std::sort() a comparator as a lambda
function which has access to the variables in the local scope, something
which is very laborious to do currently.

-- 
                                                          - Warp


Post a reply to this message

From: Darren New
Subject: Re: Standard libraries
Date: 7 Mar 2009 16:05:39
Message: <49b2e1a3$1@news.povray.org>
Warp wrote:
>   You are not discussing. You are bashing based on prejudice.

I think it's fair to say it isn't prejudice that the philosophy of C++ is 
that you don't pay for features where you don't use them. I'll admit the 
syntax seems much cleaner than I thought it would be, because I assumed 
they'd be doing something like Boost. However, C++ still doesn't have lambda 
expressions, and I'm rather at a loss why the people working on the 
standards called it that, since they aren't lambdas and they *didn't* 
include "lambda" as a keyword.

I gave examples of actual code that works in any language that has lambdas, 
and you tell me I'm not discussing C++ and that I'm prejudice. It sounds 
more like you're prejudice than me, really, defending C++ for some reason 
without actually offering any evidence I'm wrong. If I'm prejudiced and 
right, what does it matter that I'm prejudiced?

C++ sucks to the extent that you don't pay runtime overhead for features 
where you don't use them. This philosophy leads to a great number of warts 
on the language (such as delete vs delete[], & references vs && references, 
undefined behavior with non-virtual destructors in some cases, that static 
initializations aren't done in a reliable order, etc.). I can understand 
these warts, because they're focussed on performance and/or make the 
compiler writer's job easier (and hence make the language easier to 
implement and port).

The syntax also sucks, to a great extent due to the lame attempt to remain 
compatible with a different language they're already incompatible with. Had 
every C program actually been a valid C++ program with the same semantics, 
this might have made sense, but they failed to ensure this too.

I think the fact that I came to different conclusions than you do don't make 
it prejudice. I'm willing to learn, but if you can't tell me where I'm 
wrong, then telling me I'm bashing based on prejudice doesn't help.

-- 
   Darren New, San Diego CA, USA (PST)
   My fortune cookie said, "You will soon be
   unable to read this, even at arm's length."


Post a reply to this message

From: Darren New
Subject: Re: Standard libraries
Date: 7 Mar 2009 16:14:31
Message: <49b2e3b7$1@news.povray.org>
Warp wrote:
> Darren New <dne### [at] sanrrcom> wrote:
>>>> I'm biased against ugly languages that are a pile of hacks to fix flaws in 
>>>> other hacks.
>>>   At least you dare to admit it.
> 
>> Sure. I've never hesitated to express my dislike of hacks, even when I 
>> recognise them as necessary hacks.
> 
>   I said that "at least you admit that you are biased". I didn't say that
> "at least you admit you dislike something". But of course you seem quite
> fluent at twisting things.

Sure. I'm biased against ugly half-ass hacks. I never disputed that. :-)

If the ugly half-ass hacks let you do something you couldn't otherwise do, 
that's a redeeming feature. That was why I brought up Python - their ugly 
half-ass hacks let you do things you can't easily do without them, hooking 
into the bowels of the scope/name resolution at runtime. It's hacky and I 
can't help but believe inefficient, but it lets you implement things you 
couldn't otherwise implement easily.

That's why I asked why a lambda in C++ is better than just coding the body 
in the for loop - I don't see that it gains you anything, and I was 
wondering what you'd code differently.

>   You are being an unusually bad jerk in this thread for some reason. Not
> your usual self. I'm curious what prompted your sudden holy war now.
> Bad day?

Heh. Sorry if I'm coming across strong. I'm having a good day actually. And 
I honestly appreciate you asking. And yea, ok, the "grow up already" 
statement was probably unnecessarily harsh. I was a little peeved that you'd 
take a perfectly reasonable question and attribute malice to it.

I think it might be that (1) this is the first time I've bashed C++ since I 
actually sat down and learned it in detail, so it has been a number of weeks 
or months, and (2) you're accusing me of being prejudiced without apparently 
accusing me of being wrong.  I don't have to know the details of how C++ 
implements lambdas to know they're not going to add GC to C++ in order to 
support them, yes?

I 100% honestly think you're taking things personally that aren't being 
personally directed at you. I haven't said you're foolish to use C++ or 
anything else directed at you.  I simply said C++ sucks in certain ways, and 
it seems counter-intuitive to me why people as smart as those standardizing 
C++ would add features like lambdas with no obvious benefit compared to the 
pitfalls you have to avoid when you use them.

-- 
   Darren New, San Diego CA, USA (PST)
   My fortune cookie said, "You will soon be
   unable to read this, even at arm's length."


Post a reply to this message

From: Warp
Subject: Re: Standard libraries
Date: 7 Mar 2009 16:31:02
Message: <49b2e796@news.povray.org>
Darren New <dne### [at] sanrrcom> wrote:
> However, C++ still doesn't have lambda 
> expressions, and I'm rather at a loss why the people working on the 
> standards called it that, since they aren't lambdas and they *didn't* 
> include "lambda" as a keyword.

  This sounds like the same type of argument as "xyz is not an OS" vs.
"xyz is an OS", depending on your definition of "OS".

  One of the basic definition of "lambda function" in programming languages
is that they are "first-class objects". A first-class object is, usually,
defined as something you can express as a literal (or, in the case of
lambda functions, as a "nameless" function implementation), something
which you can create/instantiate "on the spot", and which you can store
in a variable.

  Some people might use the term "lambda function" as a synonym for
"nameless function".

  In the same way as something being or not being an operating system
depending on how pedantic you are about the definition, it can be fair
to say that something like the new C++ lambda functions can be said to
be or not be "true" lambda functions depending on how pedantic you want
to be about the definition.

  If you really want to be so pedantic about the definition, then by all
means. It's just rather irritating when you start mocking the language
because you don't agree with their terminology.

> It sounds 
> more like you're prejudice than me, really, defending C++ for some reason 
> without actually offering any evidence I'm wrong.

  It was not a question of being right or wrong. It was a question of your
glaringly condescending attitude, which was deliberately inflammatory.
You didn't show any respect whatsoever to people who might actually like
the language (nor to the people who are trying very hard to make it
better).

> If I'm prejudiced and 
> right, what does it matter that I'm prejudiced?

  You were not right. You started asking questions until you found
something which you could argue you are right about. You started with
prejudiced mocking without even knowing about how the feature will be
implemented. You had already decided that the feature sucks, without
even knowing anything about it.

> The syntax also sucks, to a great extent due to the lame attempt to remain 
> compatible with a different language they're already incompatible with. Had 
> every C program actually been a valid C++ program with the same semantics, 
> this might have made sense, but they failed to ensure this too.

  You could use the same argument to say that the syntax of Java and C# suck
as well.

-- 
                                                          - Warp


Post a reply to this message

From: Warp
Subject: Re: Standard libraries
Date: 7 Mar 2009 16:44:49
Message: <49b2ead1@news.povray.org>
Darren New <dne### [at] sanrrcom> wrote:
> That's why I asked why a lambda in C++ is better than just coding the body 
> in the for loop - I don't see that it gains you anything, and I was 
> wondering what you'd code differently.

  As I already mentioned, the for_each() was just a simple example. There
are many other situations where a for-loop won't do. std::sort() is a good
example.

  You could, of course, create the same effect as with the lambda function
by other means, but it would be laborious and cumbersome (eg. creating a
struct of pointers to all the local variables you want visible
inside the comparator function, and implementing the comparator inside
that struct, and then instantiating the struct and giving it to std::sort),
and most probably the compiler wouldn't even be able to optimize it as well.

  The lambda functionality makes this a lot more convenient, and probably
also more efficient.

> I 100% honestly think you're taking things personally that aren't being 
> personally directed at you.

  You still know that I get trolled when someone writes such mocking words
about C++ as you have done.

> I haven't said you're foolish to use C++ or 
> anything else directed at you.  I simply said C++ sucks in certain ways, and 
> it seems counter-intuitive to me why people as smart as those standardizing 
> C++ would add features like lambdas with no obvious benefit compared to the 
> pitfalls you have to avoid when you use them.

  I really can't understand why you don't see the benefits.

  One could argue the same thing about almost any convenience feature.
For example, one could argue that templates are useless because you can
do everything that can be done with templates by other means. Of course
you can. You can write asm and get the same functionality and be done
with it. However, your asm will certainly not be as clear and maintainable,
and certainly enormously longer than high-quality template code can be.

  Templates in C++ are very useful for many reasons. For instance, they
help avoiding code repetition when the same thing has to be done with
several types. They also help the compiler create more efficient code.
Templates also help abstracting your code so that it will become more
easy to maintain in the future. There are tons of reasons why templates
are a useful convenience feature, even if, technically speaking, you could
achieve the same end results without them (after all, C has been doing that
for quite a long time).

  Templates are in fact so powerful that entire books have been written
about them.

  Personally I see C++ lambda functions (in conjunction with things like
auto variables) having the same kind of potential.

-- 
                                                          - Warp


Post a reply to this message

From: Darren New
Subject: Re: Standard libraries
Date: 7 Mar 2009 16:49:23
Message: <49b2ebe3$1@news.povray.org>
Warp wrote:
> Darren New <dne### [at] sanrrcom> wrote:
>> Warp wrote:
>>> Darren New <dne### [at] sanrrcom> wrote:
>>>> Warp wrote:
>>>>>     std::for_each(someList, [&total](int x) { total += x });
>>>> And, really, what's the advantage of this over a normal for loop?
>>>   Nothing. Since it's C++ code, it sucks.
> 
>> You know, if you're not mature enough to have a civil conversation, you 
>> don't really need to participate.
> 
>   You really are a master of trolling, you know. I have to admire that.
> 
>   First you succeed in trolling me with your unusually heavy and biased
> C++ bashing, and then you try to make it look like all you wanted to do
> is have a simple civil conversation.

Seriously, in this post, the question was, *really*, what's the advantage?

Really. That's all I meant. That's why I said "really".  I'm not trolling. 
I'm trying to get someone who *does* understand the feature to tell me the 
advantage.

I understand the advantage of lambda expressions. I don't understand the 
advantage of what C++ calls lambda expressions over just using regular 
inline code, since the lambda expression isn't really a self-contained value 
you can return from its scope.

> Every question you made about new C++
> features were aimed solely at trying to find flaws in them. When your
> questions were answered, you asked more questions, trying to find flaws.

I'd say "limitations" rather than flaws. I tend to understand what a feature 
is intended for, and find that limitations on the feature are where I get 
tripped up.

> You continued asking questions until you found some flaw you could point
> out. (Eg. first you asked if lambda functions would use such a cumbersome
> syntax as boost::lambda, and when you got shown that they won't, you asked
> if you can assign lamdbda functions to variables, and when you got shown
> that you can, you continued asking and asking until you found something
> that you can consider a flaw so that you could mock the whole feature.

Nope. I knew C++ can't support lambdas in the usual sense of the word (as a 
mechanism for constructing closures). I was trying to figure out at what 
point it breaks down. The pointer to the part of the wikipedia page 
explaining it's undefined behavior to refer to a variable in a scope that 
has exited removed my confusion.

>   You really are good at trolling. Personally I could not get even near
> that level of mastery.

While I appreciate the complement, I really wasn't trying to piss you off. 
Maybe due to the social circles I've hung out in over the last few years, I 
tend to do such things more often than I should, unintentionally. I'm sorry 
if I've gotten your blood pressure up, and I hope I haven't disturbed your 
weekend.

I'll try to phrase things differently in the future, but even when I 
specifically disclaim I'm saying anything about you, you sometimes take 
things personally, so I'm not sure how much more I can make the questions 
sound as I mean them to be taken.

>   Another example: You could give std::sort() a comparator as a lambda
> function which has access to the variables in the local scope, something
> which is very laborious to do currently.

Hmmm. OK, thanks. I can see where something like that could be convenient. 
Most places I've seen that need that sort of thing pass in a "client data" 
sort of opaque pointer to allow it, so I guess this new stuff could obviate 
the need to design that in at the application layer.

-- 
   Darren New, San Diego CA, USA (PST)
   My fortune cookie said, "You will soon be
   unable to read this, even at arm's length."


Post a reply to this message

From: Darren New
Subject: Re: Standard libraries
Date: 7 Mar 2009 17:05:51
Message: <49b2efbf$1@news.povray.org>
Warp wrote:
>   This sounds like the same type of argument as "xyz is not an OS" vs.
> "xyz is an OS", depending on your definition of "OS".

Kind of, perhaps.

>   One of the basic definition of "lambda function" in programming languages
> is that they are "first-class objects".

Yes. But that's not the only part of the definition. You can store a C++ 
lambda in a variable, but that's not all that needs to be done to make it a 
lambda.

>   Some people might use the term "lambda function" as a synonym for
> "nameless function".

That only works if you actually close over the free variables. Unlike a lot 
of terms, lambda actually has a precise mathematical definition that was 
around before computers, and there are already terms for what C++ is calling 
lambda that are correct for what it does (e.g., "downward funarg"). They 
really are calling it the wrong thing if they call it "lambda".

>   If you really want to be so pedantic about the definition, then by all
> means. It's just rather irritating when you start mocking the language
> because you don't agree with their terminology.

Well, it's well-established terminology that means something different. It's 
like if they added "unlimited precision integers" as long as they fit in a 
machine register, because, you know, you've got unlimited precision *after* 
the decimal point.

>   It was not a question of being right or wrong. It was a question of your
> glaringly condescending attitude, which was deliberately inflammatory.

Well, only a little. :-) Point taken. I'll try to tone it down in the 
future. My apologies.

>   You were not right. You started asking questions until you found
> something which you could argue you are right about. You started with
> prejudiced mocking without even knowing about how the feature will be
> implemented. You had already decided that the feature sucks, without
> even knowing anything about it.

As I said, I knew it wasn't going to start copying stack frames to the heap 
and then GCing it later.

>   You could use the same argument to say that the syntax of Java and C# suck
> as well.

The syntax of a lot of stuff in Java *does* suck. The syntax of C# gets 
increasingly baroque as they add more features. But C++ syntax is so bad you 
can't even tell whether a line of code is a type declaration or a variable 
declaration or a function invocation without reading all the include files. 
To the point where you start getting things like the "typename" keyword 
(iirc) just to tell the compiler whether you meant to give a type or a 
variable name at that point.

You can't tell me a language with undecidable syntax isn't at least a little 
sucky, yes?

-- 
   Darren New, San Diego CA, USA (PST)
   My fortune cookie said, "You will soon be
   unable to read this, even at arm's length."


Post a reply to this message

From: Darren New
Subject: Re: Standard libraries
Date: 7 Mar 2009 17:14:02
Message: <49b2f1aa$1@news.povray.org>
Warp wrote:
>   You could, of course, create the same effect as with the lambda function
> by other means, but it would be laborious and cumbersome (eg. creating a
> struct of pointers to all the local variables you want visible
> inside the comparator function, and implementing the comparator inside
> that struct, and then instantiating the struct and giving it to std::sort),
> and most probably the compiler wouldn't even be able to optimize it as well.

That's actually how C# implements lambdas. Except that with GC available, 
you can then return the lambda and the local variables (in all the stack 
frames above that you've closed over) still exist until you GC the lambda 
expression.  Just so's ya know.

>   The lambda functionality makes this a lot more convenient, and probably
> also more efficient.

Certainly more convenient, yes.

>> I 100% honestly think you're taking things personally that aren't being 
>> personally directed at you.
> 
>   You still know that I get trolled when someone writes such mocking words
> about C++ as you have done.

I'll sincerely try to sound less mocking in the future.

>   I really can't understand why you don't see the benefits.

Now that you've given me some examples to think about, I do. I was thinking 
about the convenience of actual lambda functions that I've taken advantage 
of, none of which are supported by what C++ calls lambda functions. Now that 
you describe some examples where C++ lambdas are convenient, I can visualize 
places where I've worked around the lack of that sort of feature.

For example, in Tcl, when you initialize a DLL written in C, you get to 
return a void* from the initialization feature, and that void* gets carried 
around by the interpreter and passed back into whatever functions that DLL 
declared as Tcl-accessible. So that's the same sort of situation in which 
using a downward funargs in C++ would be helpful. And it's not a lambda 
either. :-)

I probably write different kinds of code than you do, but it doesn't really 
come up that often in stuff I write in an OO language. Maybe because the 
stuff that I'd normally be closing over I'm instead holding in member 
variables, so the "this" gets passed around anyway for context.

>   Personally I see C++ lambda functions (in conjunction with things like
> auto variables) having the same kind of potential.

I think C++ templates are far more powerful than C++ lambdas. Altho lambdas 
that actually do the closure stuff are pretty powerful too. (For example, 
you can define the integers using nothing but them. :-)

-- 
   Darren New, San Diego CA, USA (PST)
   My fortune cookie said, "You will soon be
   unable to read this, even at arm's length."


Post a reply to this message

From: Nicolas Alvarez
Subject: Re: Standard libraries
Date: 7 Mar 2009 17:33:54
Message: <49b2f651@news.povray.org>
Darren New wrote:
> I can pass you a void* and

That's a C feature. Real C++ programmers don't use void*. So no, of course
you have no RTTI on a void*.

> The traceback in your exceptions
> can tell you what the values passed as arguments to the function on each
> frame...

Yes they can.

> Hell, you can't even tell me how big an array is

No, but you'll see me using std::vector, not arrays, in C++.

> I know how it *won't* be implemented. It won't be implemented such that
> references to local variables survive the end of the stack frame those
> local variables are allocated in. That's enough.

They never do, and programmers better know that... They are "local
variables" for a reason. And in my mind it wouldn't be a stack if it kept
alive after 'popped' :D

int& foo() {
   int myNumber = 42;
   return myNumber;
}

Undefined behavior when the caller tries to do something with the reference.


Post a reply to this message

From: Nicolas Alvarez
Subject: Re: Standard libraries
Date: 7 Mar 2009 17:39:52
Message: <49b2f7b8@news.povray.org>
Darren New wrote:
> Warp wrote:
>>     std::for_each(someList, [&total](int x) { total += x });
> 
> And, really, what's the advantage of this over a normal for loop?

You tell me.

int total = 0;
std::vector<int> someList;

std::for_each(someList, [&total](int x) {
   total += x;
});

vs

int total = 0;
std::vector<int> someList;

for (std::vector<int>::const_iterator i = someList.begin(); i !=
someList.end(); ++i) {
    total += *i;
}


Post a reply to this message

From: Warp
Subject: Re: Standard libraries
Date: 7 Mar 2009 17:46:46
Message: <49b2f956@news.povray.org>
Darren New <dne### [at] sanrrcom> wrote:
> But C++ syntax is so bad you 
> can't even tell whether a line of code is a type declaration or a variable 
> declaration or a function invocation without reading all the include files. 

  That's also one of the great assets of the language. Consider this:

template<typename F>
void foo()
{
    F(5);
}

  Is that F(5) a function call or is it instantiating a variable of type F?
It's impossible to tell without knowing what F might be.

  However, it doesn't matter! As long as F behaves like a function, that's
just fine. This is used quite a lot in template coding, eg. with functors
and comparators. This gives flexibility and versatility.

  It's not like C++ was the only language which behaves like this.

> To the point where you start getting things like the "typename" keyword 
> (iirc) just to tell the compiler whether you meant to give a type or a 
> variable name at that point.

  Most languages have special keywords to denote what you want to do.
For example, some languages might have a keyword like "function" to denote
that you are starting a function definition, as opposed to starting a
function call. I don't really see the big difference.

> You can't tell me a language with undecidable syntax isn't at least a little 
> sucky, yes?

  When that syntax brings flexibility, I don't consider is such a huge
problem.

-- 
                                                          - Warp


Post a reply to this message

From: Warp
Subject: Re: Standard libraries
Date: 7 Mar 2009 17:51:22
Message: <49b2fa6a@news.povray.org>
Nicolas Alvarez <nic### [at] gmailcom> wrote:
> int& foo() {
>    int myNumber = 42;
>    return myNumber;
> }

> Undefined behavior when the caller tries to do something with the reference.

  I think there's a reason why you can't have references to primitive
types in Java.
  (Sometimes this can be a bit limiting, eg. because you can't pass
primitive types by reference to functions, for them to modify the
original values rather than copies.)

-- 
                                                          - Warp


Post a reply to this message

From: Nicolas Alvarez
Subject: Re: Standard libraries
Date: 7 Mar 2009 17:52:37
Message: <49b2fab5@news.povray.org>
Darren New wrote:
> Warp wrote:
>>   Another example: You could give std::sort() a comparator as a lambda
>> function which has access to the variables in the local scope, something
>> which is very laborious to do currently.
> 
> Hmmm. OK, thanks. I can see where something like that could be convenient.
> Most places I've seen that need that sort of thing pass in a "client data"
> sort of opaque pointer to allow it, so I guess this new stuff could
> obviate the need to design that in at the application layer.

In C, the standard qsort() function takes a function pointer and (I think)
a "client data" void pointer. You have to put your state in a struct, send
the pointer as client data, then in your comparator function get the data
through the pointer.

In C++, the standard std::sort template function takes a comparator functor.
Using functors (which can be an instance of a stateful class, or can be a
plain old function) instead of passing a "client data" pointer is quite
common in C++.

Here is a [useless in practice] example where the comparator takes an
initialization parameter (whether to sort descending or ascending), and
also keeps state that can be retrieved later (how many times the comparison
function was called).

struct MyComparator {
    bool reverse_mode;
    int comparison_count;

    MyComparator(bool reverse) {
        reverse_mode = reverse;
        comparison_count = 0;
    }

    bool operator()(int a, int b) {
        comparison_count++;
        if (reverse_mode) {
            return b < a;
        } else {
            return a < b;
        }
    }
};

std::vector<int> list;

MyComparator c(false);
std::sort(list.begin(), list.end(), c);

cout << "Comparator called " << c.comparison_count << " times\n";

If you don't need to retrieve any data after the comparison, you could also
do this:

std::sort(list.begin(), list.end(), MyComparator(false));


With lambdas, you can use "closures" to keep state, so you don't need a
separate class/struct declared elsewhere:

std::vector<int> list;
int comparison_count=0;

std::sort(list.begin(), list.end(), [&comparison_count](int a, int b) {
    comparison_count++;
    return a < b;
});

Even if you don't want to keep any state (and leave the [] empty to use no
closures), it's still useful to declare the comparison code right next to
the std::sort call, instead of in a function/class possibly far away in
the .cpp file. This is the part I said was similar to string literals (you
know how in assembler you need a manually-made list of strings at the
beginning of the program? :P).


Post a reply to this message

From: Warp
Subject: Re: Standard libraries
Date: 7 Mar 2009 17:55:44
Message: <49b2fb70@news.povray.org>
Nicolas Alvarez <nic### [at] gmailcom> wrote:
> for (std::vector<int>::const_iterator i = someList.begin(); i !=
> someList.end(); ++i) {
>     total += *i;
> }

  Maybe not the best of examples because with the next C++ standard you
will also be able to do it like:

    for(auto i = someList.begin(); i != someList.end(); ++i)
        total += *i;

which is not enormously longer than the lambda version.

-- 
                                                          - Warp


Post a reply to this message

From: Nicolas Alvarez
Subject: Re: Standard libraries
Date: 7 Mar 2009 17:58:49
Message: <49b2fc29@news.povray.org>
Warp wrote:
> Nicolas Alvarez <nic### [at] gmailcom> wrote:
>> for (std::vector<int>::const_iterator i = someList.begin(); i !=
>> someList.end(); ++i) {
>>     total += *i;
>> }
> 
>   Maybe not the best of examples because with the next C++ standard you
> will also be able to do it like:
> 
>     for(auto i = someList.begin(); i != someList.end(); ++i)
>         total += *i;
> 
> which is not enormously longer than the lambda version.

Yes, when changing C++ code to C++0x, I'll have to decide which of the two
types of awesome to use.


Post a reply to this message

From: Darren New
Subject: Re: Standard libraries
Date: 7 Mar 2009 20:19:43
Message: <49b31d2f$1@news.povray.org>
Warp wrote:
>   However, it doesn't matter! As long as F behaves like a function, that's
> just fine. This is used quite a lot in template coding, eg. with functors
> and comparators. This gives flexibility and versatility.

Hmm. I never thought of it that way. OK.

>   It's not like C++ was the only language which behaves like this.

Well.... I think it's one of the few languages where you can't tell whether 
something's invoking a function or adding a name to the namespace at compile 
time.  It would seem to make writing parsers and such harder than it needs 
to be.

I'll grant that there are a number of languages where (say) writing "F(5)" 
doesn't tell you whether F is a function, a pointer to a function, or a 
function that returns another function, say.

>   Most languages have special keywords to denote what you want to do.
> For example, some languages might have a keyword like "function" to denote
> that you are starting a function definition, as opposed to starting a
> function call. I don't really see the big difference.

It makes the grammar easier to automate.

>> You can't tell me a language with undecidable syntax isn't at least a little 
>> sucky, yes?
> 
>   When that syntax brings flexibility, I don't consider is such a huge
> problem.

Other languages seem to manage being as flexible without having such complex 
grammars. I'll grant that they trade off runtime efficiency to achieve that.

-- 
   Darren New, San Diego CA, USA (PST)
   My fortune cookie said, "You will soon be
   unable to read this, even at arm's length."


Post a reply to this message

From: Darren New
Subject: Re: Standard libraries
Date: 7 Mar 2009 20:31:38
Message: <49b31ffa$1@news.povray.org>
Nicolas Alvarez wrote:
> This is the part I said was similar to string literals (you
> know how in assembler you need a manually-made list of strings at the
> beginning of the program? :P).

Oh! OK.  I had no idea that's what you were talking about, and I thought you 
were comparing lambda expressions to string literals somehow.

Thanks for the excellent examples.

-- 
   Darren New, San Diego CA, USA (PST)
   My fortune cookie said, "You will soon be
   unable to read this, even at arm's length."


Post a reply to this message

From: Warp
Subject: Re: Standard libraries
Date: 7 Mar 2009 20:34:13
Message: <49b32095@news.povray.org>
Darren New <dne### [at] sanrrcom> wrote:
> Well.... I think it's one of the few languages where you can't tell whether 
> something's invoking a function or adding a name to the namespace at compile 
> time.  It would seem to make writing parsers and such harder than it needs 
> to be.

  Who cares if writing a parser is harder? Programs are supposed to help
people, not the other way around. I prefer latex over xml any day, even
though creating a latex parser is way more complicated than creating an
xml parser.

-- 
                                                          - Warp


Post a reply to this message

From: Darren New
Subject: Re: Standard libraries
Date: 7 Mar 2009 23:29:42
Message: <49b349b6$1@news.povray.org>
Warp wrote:
>   Who cares if writing a parser is harder? 

Well, anyone implementing compilers or interpreters for one. If the compiler 
gave you runtime type information sufficient for everyone's needs, that 
would probably be enough. But if you want to (say) write something that'll 
take a structure and store it in database tables, or serialize something to 
a file or a SOAP message or some such, not having the type information *and* 
not being able to easily parse the header files can be problematic.

-- 
   Darren New, San Diego CA, USA (PST)
   My fortune cookie said, "You will soon be
   unable to read this, even at arm's length."


Post a reply to this message

From: Darren New
Subject: Re: Standard libraries
Date: 8 Mar 2009 00:09:20
Message: <49b35300$1@news.povray.org>
Warp wrote:
>   You still know that I get trolled when someone writes such mocking words
> about C++ as you have done.

You know, I took a couple hours away, then went back and looked at what I'd 
typed. I think if you look at my initial posts here objectively, you'll see 
they weren't mocking, as almost all of it was asking questions about the 
implementation.

What I failed to do was describe the mental process that caused me to ask 
the kinds of apparently-silly questions I was asking. I thought

(( Wow, C++ is getting lambdas. I guess they're standardizing Boost, which 
is ugly. )) "I bet it'll be ugly."

(( No, it actually *looks* not too bad. But I wonder how they implement it, 
then. That's a mess behind the scenes in C#, and in pretty much every 
language I know that keeps variables on the machine stack, so I wonder how 
they're doing closures. I wonder how the closed-over variables survive the 
destruction of the stack frame. ))

"Can you return lambda values from a function?"  (( Because if not, then you 
don't have to worry about the stack frame not being there. Oh, but I'm told 
you can. Cool. Maybe it's going to copy the values from the stack into a 
heap-allocated object. But how would it GC the heap-allocated object, if 
multiple lambdas point to it? Maybe it's doing reference counting. But that 
doesn't work, if you get circular references, which you can't get if you 
can't assign to a variable except a new variable. ))

"Can you assign it to a variable?"  (( Oh, you can do that too. Wow, that's 
really impressive. I can't imagine what they're doing to make closures work.))

"Thanks for the link to wikipedia."  (( Oh, wikipedia says you just randomly 
get hosed if you try to actually use values you thought you closed over when 
you created the lambda, unlike every other language that ever implemented 
the feature called 'lambda expressions'. ))

"Gee, C++ lambdas sure suck compared to everyone else's that actually 
manages to create a closure from lambdas."  (( I wonder what it could be 
useful for, since I can't think of a reason I'd use a lambda that I couldn't 
return from the function in which I created it. ))

"Why not just use a for loop?"  (( Oh, I see. I wasn't thinking like that, 
because I was still thinking in terms of lambdas rather than in terms of 
downward funargs. ))

I can see how if I didn't make clear why I was asking the questions, you 
might leap to the conclusion (perhaps justifiably) that I was simply looking 
for the flaws in order to criticize C++.  In reality, I was looking for the 
flaw in my mental model of how C++ handles memory that would allow the 
memory for the closure to be collected only after the lambda has been 
collected. Or, given that I'm pretty sure I understand how C++ works its 
memory model, I was looking for the cool impressive clever mechanism that 
would let the closure get allocated in a way that would work.

-- 
   Darren New, San Diego CA, USA (PST)
   My fortune cookie said, "You will soon be
   unable to read this, even at arm's length."


Post a reply to this message

From: Darren New
Subject: Re: Standard libraries
Date: 8 Mar 2009 00:09:21
Message: <49b35301$1@news.povray.org>
Nicolas Alvarez wrote:
> Darren New wrote:
>> I can pass you a void* and
> 
> That's a C feature. Real C++ programmers don't use void*. So no, of course
> you have no RTTI on a void*.

OK. What am I doing wrong here, that I'm not getting the size of the actual 
instance passed in?

#include <iostream>

class Alpha {
   public:
   int i;
   Alpha() { i = 9; }
   virtual ~Alpha() { }
   virtual int yidda() { return 0; }
   virtual int mysize() { return sizeof(*this); }
};

class Beta : public Alpha {
   public:
   long l0, l1, l2, l3, l4, l5, l6, l7;
   Beta() { i = 10; }
   virtual ~Beta() { }
   virtual int yidda() { return 1; }
};

void show_size(Alpha & xyz) {
   std::cout << sizeof(xyz) << std::endl;
   std::cout << xyz.mysize << std::endl;
}

int main() {
   Alpha alpha;
   Beta  beta;
   show_size(alpha);
   show_size(beta);
   return 0;
}

Why does this print "8 8 8 8"? If there's RTTI, shouldn't I at least be able 
to figure out how big my structure is without having to code a function to 
calculate it for every function?  Seriously, if you wanted to write whatever 
structure "show_size" got passed out to a file, how would you do it? Would 
you have to write a "return sizeof(*this)" in each and every class as a 
virtual function? A function inherited by the parent into the child doesn't 
know that "this" is pointing to a child type?  (I tried adding more longs to 
alpha and I started getting 32's, so it's not giving me the size of the 
reference itself.)

If I copy the text of mysize() into Beta's class, mysize() returns 40 for 
betas. That seems kind of ... not right, to me. I duplicate a virtual 
function into a child class without changing it, and it gives different 
answers than if I inherited it?

>> The traceback in your exceptions
>> can tell you what the values passed as arguments to the function on each
>> frame...
> 
> Yes they can.

I wasn't aware C++ even defined tracebacks on exceptions. How does one 
access that feature? The googles with the obvious words mostly talk about 
python exceptions passing through C++, and the g++ exception.h file declares 
exception as having a constructor, destructor, and a "what" field, but no 
traceback. What should I google on to find the right invocation to get the 
traceback out of an exception?

>> Hell, you can't even tell me how big an array is
> No, but you'll see me using std::vector, not arrays, in C++.

Yes, because you can't see how big an array is.

>> I know how it *won't* be implemented. It won't be implemented such that
>> references to local variables survive the end of the stack frame those
>> local variables are allocated in. That's enough.
> 
> They never do,

Well, no, not in C++. That's kind of my point. In other languages with 
lambdas, yes, they do. That's why it's called a closure. That's why I was 
asking stupid questions about how C++ manages to do that. :-)

> And in my mind it wouldn't be a stack if it kept
> alive after 'popped' :D

Well, yes. That's exactly why I'm asking "Wow, how does C++ do that??" 
Because, like, that's what "lambda" means, and C++ can't do it, so I'm 
trying to figure out what amazing mechanism they used.

-- 
   Darren New, San Diego CA, USA (PST)
   My fortune cookie said, "You will soon be
   unable to read this, even at arm's length."


Post a reply to this message

From: Warp
Subject: Re: Standard libraries
Date: 8 Mar 2009 12:03:02
Message: <49b3ec36@news.povray.org>
Darren New <dne### [at] sanrrcom> wrote:
> OK. What am I doing wrong here, that I'm not getting the size of the actual 
> instance passed in?

[snip]

> Why does this print "8 8 8 8"? If there's RTTI, shouldn't I at least be able 
> to figure out how big my structure is without having to code a function to 
> calculate it for every function?

  The problem is that sizeof() is a static construct which is evaluated at
compile time, not something evaluated at runtime. sizeof() in particular
does *not* take into accout RTTI. When you say "sizeof(*this)" (in your
example), that's by standard specification exactly identical to
"sizeof(Alpha)".

  But C++ does have RTTI. For example try this:


#include <iostream>
#include <string>
#include <typeinfo>

class Alpha
{
 public:
    virtual ~Alpha() {}
    std::string myType() const { return typeid(*this).name(); }
};

class Beta: public Alpha
{
};

int main()
{
    Alpha obj1;
    Beta obj2;
    Alpha& obj3 = obj2;

    std::cout << obj1.myType() << " "
              << obj2.myType() << " "
              << obj3.myType() << std::endl;
}


  Even though the class Alpha does not see the class Beta in any way (you
could make completely sure of that by putting them in different compilation
units), it will still return the correct typeid name depending on whether
the object is really of type Alpha or of type Beta.

-- 
                                                          - Warp


Post a reply to this message

From: Darren New
Subject: Re: Standard libraries
Date: 8 Mar 2009 13:29:03
Message: <49b4005f$1@news.povray.org>
Warp wrote:
> units), it will still return the correct typeid name depending on whether
> the object is really of type Alpha or of type Beta.

Does the typeid tell you anything but basically the name? It looks like 
there's just a textual name and a total (but otherwise undefined) ordering 
on types. No way to actually use the type information to do something I'd 
consider useful.  Is this the sort of thing that compilers extend, or is the 
only actual use to get the name of the type or compare types in a template?

It doesn't really look like the type information lets you do much with the 
contents of the type. It doesn't tell you anything about the type - it just 
tells you the ID of the type. It doesn't tell you how big it is, or which 
members are pointers, or anything like that that would let you do something 
like serialize the data out to a file. That's usually what I think of when I 
hear "run-time type information". Not "I can compare two values in a 
statically-typed language and see if they're the same type." :-)

-- 
   Darren New, San Diego CA, USA (PST)
   My fortune cookie said, "You will soon be
   unable to read this, even at arm's length."


Post a reply to this message

From: Warp
Subject: Re: Standard libraries
Date: 8 Mar 2009 15:23:29
Message: <49b41b31@news.povray.org>
Darren New <dne### [at] sanrrcom> wrote:
> It doesn't really look like the type information lets you do much with the 
> contents of the type. It doesn't tell you anything about the type - it just 
> tells you the ID of the type. It doesn't tell you how big it is, or which 
> members are pointers, or anything like that that would let you do something 
> like serialize the data out to a file. That's usually what I think of when I 
> hear "run-time type information". Not "I can compare two values in a 
> statically-typed language and see if they're the same type." :-)

  "Doing something depending on which type of object *this is" is precisely
what virtual functions are for, and that's the RTTI C++ has. Additionally,
you can get a unique ID for each type of object (resolvable at runtime,
as demonstrated), if you find a use for that. (One possible use could be
that objects could register themselves to some centralized, non-template
factory, using their own typeid as key, so that the factory could create
clones of those objects at runtime, based on those typeids.)

  "It doesn't tell you how big it is, or which members are pointers, or
anything like that" doesn't sound very OO to me. The whole idea of objects
is that they are abstract: You don't know what they have inside, nor do
you even need to know.

  I surely can understand why such information could sometimes be useful,
but it still sounds to me like you are accessing the internals of an object,
bypassing its public interface, which is something that breaks the basic
idea of modularity.

-- 
                                                          - Warp


Post a reply to this message

From: Darren New
Subject: Re: Standard libraries
Date: 8 Mar 2009 16:53:45
Message: <49b43059$1@news.povray.org>
Warp wrote:
>   "Doing something depending on which type of object *this is" is precisely
> what virtual functions are for, and that's the RTTI C++ has.

OK. I guess "RTTI" and reflection aren't really as related as I thought they 
were. I find languages without metaclass-style operations are exceedingly 
tedious to work with, in the kinds of stuff I usually write. Hence my 
obvious disdain for the limited RTTI that C++ provides. However, yes, I'll 
grant you that virtual functions are indeed based on run-time type 
information. I'd just prefer a version that doesn't require me to 
reimplement in code all the stuff I already told the compiler about.

I'm kind of surprised that copying a virtual function unchanged into a child 
that inherits it changes its functionality.  That seems completely counter 
to the whole idea, to me.

>   I surely can understand why such information could sometimes be useful,
> but it still sounds to me like you are accessing the internals of an object,
> bypassing its public interface, which is something that breaks the basic
> idea of modularity.

You certainly are, when you use features like that, yes. But when the 
interface is made available to do that safely, it provides a lot of 
benefits. It's not how you want to normally interact with an object, but 
rather part of a meta-protocol for interacting with arbitrary objects. It's 
no different really than having a mix-in class, or having methods on the 
base "object" class (for single-rooted languages).

I.e., in much the same way that templates keep you from having to write the 
same code for lots of different classes, things like reflection prevent you 
from having to write the same code *inside* lots of different classes. 
There's nothing that goes on with reflection that you couldn't code manually 
as virtual functions in each class. The reflection just lets you put into a 
library some kinds of functions that would otherwise be scattered into each 
class that wants to use them. For example, with reflection, I could expose a 
class via COM (which is quite object-oriented), and do so without having to 
manually write the COM wrapper - write once, use lots. It's basically 
looking at the same information you already gave the compiler once, and it 
keeps you from having to duplicate the declarations in some second language 
(like string literals or AST structures or ...) so that you can access them.

It's pretty much exactly like sizeof() in C, which lets you invoke malloc() 
without having to do a lot of manual stuff for each particular type of 
struct you want to malloc. It's just looking at compile-time information and 
packaging it up in a way that lets you examine it. It would be kind of silly 
in C to say "you have to manually count up how many bytes you use and store 
that somewhere if you want to allocate a structure on the heap", when you've 
already told the compiler what's in the structure.

Of course, there would be some runtime overhead involved, like storing a 
description of the class (as an AST perhaps) in the executable file.

-- 
   Darren New, San Diego CA, USA (PST)
   My fortune cookie said, "You will soon be
   unable to read this, even at arm's length."


Post a reply to this message

From: Nicolas Alvarez
Subject: Re: Standard libraries
Date: 8 Mar 2009 17:38:03
Message: <49b43abb@news.povray.org>
Darren New wrote:
> Well.... I think it's one of the few languages where you can't tell
> whether something's invoking a function or adding a name to the namespace
> at compile
> time.  It would seem to make writing parsers and such harder than it needs
> to be.
> 
> I'll grant that there are a number of languages where (say) writing "F(5)"
> doesn't tell you whether F is a function, a pointer to a function, or a
> function that returns another function, say.

In Python, f(5) could be a normal function call if f is a function, a method
call if f is a bound method object, a method call if f is an instance of a
class with a __call__ method, or an object instantiation (passing 5 to the
__init__ method) if f is a class name.


Post a reply to this message

From: Nicolas Alvarez
Subject: Re: Standard libraries
Date: 8 Mar 2009 17:40:58
Message: <49b43b6a@news.povray.org>
Darren New wrote:
> Nicolas Alvarez wrote:
>> Darren New wrote:
>>> The traceback in your exceptions
>>> can tell you what the values passed as arguments to the function on each
>>> frame...
>> 
>> Yes they can.
> 
> I wasn't aware C++ even defined tracebacks on exceptions.

It doesn't.

> How does one access that feature?

Through your debugger :)


Post a reply to this message

From: Darren New
Subject: Re: Standard libraries
Date: 8 Mar 2009 17:49:25
Message: <49b43d65$1@news.povray.org>
Nicolas Alvarez wrote:
> Darren New wrote:
>> Well.... I think it's one of the few languages where you can't tell
>> whether something's invoking a function or adding a name to the namespace
>> at compile
>> time.  It would seem to make writing parsers and such harder than it needs
>> to be.
>>
>> I'll grant that there are a number of languages where (say) writing "F(5)"
>> doesn't tell you whether F is a function, a pointer to a function, or a
>> function that returns another function, say.
> 
> In Python, f(5) could be a normal function call if f is a function, a method
> call if f is a bound method object, a method call if f is an instance of a
> class with a __call__ method, or an object instantiation (passing 5 to the
> __init__ method) if f is a class name.

That's the language I was thinking of, primarily, yes. :-) But in all those 
cases, it's a call. It's not changing the structure of the program.

In C++, you can write
   alpha beta(gamma);
and not know whether that's a declaration or executable code. Most languages 
you can tell what's a declaration and what's executable code without reading 
all previous declarations.

-- 
   Darren New, San Diego CA, USA (PST)
   My fortune cookie said, "You will soon be
   unable to read this, even at arm's length."


Post a reply to this message

From: Darren New
Subject: Re: Standard libraries
Date: 8 Mar 2009 17:53:15
Message: <49b43e4b$1@news.povray.org>
Nicolas Alvarez wrote:
> Darren New wrote:
>> Nicolas Alvarez wrote:
>>> Darren New wrote:
>>>> The traceback in your exceptions
>>>> can tell you what the values passed as arguments to the function on each
>>>> frame...
>>> Yes they can.
>> I wasn't aware C++ even defined tracebacks on exceptions.
> 
> It doesn't.

Then the traceback in your exceptions can't tell you the values, can it?

>> How does one access that feature?
> Through your debugger :)

I don't really want to tell my customers to run my production code under a 
debugger and copy/paste the traceback into the logfile before sending it to me.

-- 
   Darren New, San Diego CA, USA (PST)
   My fortune cookie said, "You will soon be
   unable to read this, even at arm's length."


Post a reply to this message

From: Nicolas Alvarez
Subject: Re: Standard libraries
Date: 8 Mar 2009 17:57:59
Message: <49b43f67@news.povray.org>
Darren New wrote:
> In C++, you can write
>    alpha beta(gamma);
> and not know whether that's a declaration or executable code. Most
> languages you can tell what's a declaration and what's executable code
> without reading all previous declarations.

You're calling alpha constructor. I don't see how that's different from
Python class creation syntax, calling __init__.


Post a reply to this message

From: Nicolas Alvarez
Subject: Re: Standard libraries
Date: 8 Mar 2009 18:01:35
Message: <49b4403e@news.povray.org>
Darren New wrote:
> Nicolas Alvarez wrote:
>> Darren New wrote:
>>> I wasn't aware C++ even defined tracebacks on exceptions.
>> 
>> It doesn't.
> 
> Then the traceback in your exceptions can't tell you the values, can it?

Exceptions have no "traceback" directly visible at runtime from inside the
code. You can use platform-specific code in an exception handler to get a
stack trace (by following stack pointers).

>>> How does one access that feature?
>> Through your debugger :)
> 
> I don't really want to tell my customers to run my production code under a
> debugger and copy/paste the traceback into the logfile before sending it
> to me.

KDE has a crash handler similar to Windows's, except it can't automatically
send the report back to the developer. It gives you a stacktrace, including
all the function parameters, and lets you save the file to upload to the
bug tracker.

When an application crashes, the crash handler *attaches* a debugger to the
just-segfaulted app, loads the debugging symbols from a different file,
gets a stacktrace, and lets the crashed app die and R.I.P.


Post a reply to this message

From: Darren New
Subject: Re: Standard libraries
Date: 8 Mar 2009 19:04:06
Message: <49b44ee6$1@news.povray.org>
Nicolas Alvarez wrote:
> Darren New wrote:
>> In C++, you can write
>>    alpha beta(gamma);

> You're calling alpha constructor.

How do you know you're not declaring a function beta that take an argument 
of type gamma and returns an alpha?

-- 
   Darren New, San Diego CA, USA (PST)
   My fortune cookie said, "You will soon be
   unable to read this, even at arm's length."


Post a reply to this message

From: Darren New
Subject: Re: Standard libraries
Date: 8 Mar 2009 19:06:09
Message: <49b44f61$1@news.povray.org>
Nicolas Alvarez wrote:
> Darren New wrote:
>> Nicolas Alvarez wrote:
>>> Darren New wrote:
>>>> I wasn't aware C++ even defined tracebacks on exceptions.
>>> It doesn't.
>> Then the traceback in your exceptions can't tell you the values, can it?
> 
> Exceptions have no "traceback" directly visible at runtime from inside the
> code.

That's what I was saying.

> You can use platform-specific code in an exception handler to get a
> stack trace (by following stack pointers).

And that's what I asked - what would I google on to see this, given it's not 
defined in exception.h?  What's it called?

> When an application crashes, the crash handler *attaches* a debugger to the
> just-segfaulted app, loads the debugging symbols from a different file,
> gets a stacktrace, and lets the crashed app die and R.I.P.

Of course, it helps that it's crashing, instead of trying to actually do 
something about the problem, I suppose.  And that most systems will have a 
debugger installed already.

-- 
   Darren New, San Diego CA, USA (PST)
   My fortune cookie said, "You will soon be
   unable to read this, even at arm's length."


Post a reply to this message

From: Nicolas Alvarez
Subject: Re: Standard libraries
Date: 8 Mar 2009 19:20:41
Message: <49b452c9@news.povray.org>
Darren New wrote:
> And that's what I asked - what would I google on to see this, given it's
> not defined in exception.h?  What's it called?

It's not directly related to exceptions, since it's a C API (not C++).

GNU libc (not libstdc++!) has C functions to get backtraces at any moment.
It's a GNU extension. You may very well call it in an exception handler.

http://www.gnu.org/software/libtool/manual/libc/Backtraces.html

The Windows API has similar functionality through the DlgHelp module,
especially the StackWalk64() and SymFromAddr() functions.


Post a reply to this message

From: Darren New
Subject: Re: Standard libraries
Date: 8 Mar 2009 19:31:53
Message: <49b45569$1@news.povray.org>
Warp wrote:
> that objects could register themselves to some centralized, non-template
> factory, using their own typeid as key, so that the factory could create
> clones of those objects at runtime, based on those typeids.)


Actually, wouldn't this be clearer to do with virtual functions or pointers 
to functions anyway? Where does typeid make things better in that scenario? 
Isn't it just as easy to do something like

class Alpha : Beta {
    ....
    virtual const char * me() { return "Alpha:Beta"; }
    ....
}

and switch on that?  That even gives you a defined value, so you can (say) 
write it into a file and read it back on the next run to reload objects that 
serialized themselves or something. Plus, you avoid the overhead of putting 
RTTI type_info on *every* class, most of which probably won't be registered 
that way?

-- 
   Darren New, San Diego CA, USA (PST)
   My fortune cookie said, "You will soon be
   unable to read this, even at arm's length."


Post a reply to this message

From: Nicolas Alvarez
Subject: Re: Standard libraries
Date: 8 Mar 2009 19:33:48
Message: <49b455dc@news.povray.org>
Darren New wrote:
> Warp wrote:
>> that objects could register themselves to some centralized, non-template
>> factory, using their own typeid as key, so that the factory could create
>> clones of those objects at runtime, based on those typeids.)
> 
> Actually, wouldn't this be clearer to do with virtual functions or
> pointers to functions anyway? Where does typeid make things better in that
> scenario? Isn't it just as easy to do something like
> 
> class Alpha : Beta {
>     ....
>     virtual const char * me() { return "Alpha:Beta"; }
>     ....
> }
> 
> and switch on that?  That even gives you a defined value, so you can (say)
> write it into a file and read it back on the next run to reload objects
> that serialized themselves or something.

I think the most useful feature of RTTI is not typeid() to get a string, but
dynamic_cast.

void foo(Alpha* ptr) {
    Beta* beta = dynamic_cast<Beta*>(ptr);
}

If ptr is a Beta object that was (maybe implicitly) cast to a Alpha*, then
beta is now equal to ptr (except its type is Beta*, so you can use Beta
members). If ptr actually pointed to an Alpha, or a Delta (another subclass
of Alpha), then the cast will throw a std::bad_cast exception.

In other words, dynamic_cast behaves like Java typecasts. We just call it
std::bad_cast instead of java.lang.ClassCastException.


Post a reply to this message

From: Darren New
Subject: Re: Standard libraries
Date: 8 Mar 2009 19:39:43
Message: <49b4573f@news.povray.org>
Nicolas Alvarez wrote:
> GNU libc (not libstdc++!) has C functions to get backtraces at any moment.
> It's a GNU extension. You may very well call it in an exception handler.
> 
> http://www.gnu.org/software/libtool/manual/libc/Backtraces.html

It would seem that's rather not very useful in the exception handler? Or am 
I misunderstanding?  If I catch a division-by-zero in main() and I want to 
log what function that happened in, how would I do that?

I guess what I'm asking is, isn't it the case that by the time you're in the 
exception handler, the stack frames leading to the unexpected exception have 
already been cleaned up, and all the stuff that's on the stack is working 
correctly?

If I have a server, and I want to catch and log failures, in most languages 
I do something along the lines of
  main() {
    try {
      do_main_loop();
    } except Exception e {
      log("Uncaught exception", e);
    } finally {
      clean_up();
    }
  }
but by the time I get to the log() call, the stack is gone, yes? Where would 
I put the call to "backtrace" such that I'd catch the actual function that 
threw the exception?

-- 
   Darren New, San Diego CA, USA (PST)
   My fortune cookie said, "You will soon be
   unable to read this, even at arm's length."


Post a reply to this message

From: Nicolas Alvarez
Subject: Re: Standard libraries
Date: 8 Mar 2009 19:47:21
Message: <49b45909@news.povray.org>
Darren New wrote:
> Nicolas Alvarez wrote:
>> GNU libc (not libstdc++!) has C functions to get backtraces at any
>> moment. It's a GNU extension. You may very well call it in an exception
>> handler.
>> 
>> http://www.gnu.org/software/libtool/manual/libc/Backtraces.html
> 
> It would seem that's rather not very useful in the exception handler? Or
> am I misunderstanding?  If I catch a division-by-zero in main() and I want
> to log what function that happened in, how would I do that?

Uh... Actually you're right :)

I dunno... I think you might be able to do something like this:

template<typename E>
void my_throw_exception(E& exc) {
    //do the backtrace() dance and store the results somewhere in exc
    throw exc;
}

But that's a bit cumbersome.

(PS: Division by zero doesn't throw a C++ exception)


Post a reply to this message

From: Fredrik Eriksson
Subject: Re: Standard libraries
Date: 8 Mar 2009 19:49:45
Message: <op.uqhy5foy7bxctx@e6600>
On Mon, 09 Mar 2009 00:33:32 +0100, Nicolas Alvarez  
<nic### [at] gmailcom> wrote:
>
> void foo(Alpha* ptr) {
>     Beta* beta = dynamic_cast<Beta*>(ptr);
> }
>
> If ptr is a Beta object that was (maybe implicitly) cast to a Alpha*,  
> then
> beta is now equal to ptr (except its type is Beta*, so you can use Beta
> members). If ptr actually pointed to an Alpha, or a Delta (another  
> subclass
> of Alpha), then the cast will throw a std::bad_cast exception.

No. A failed dynamic_cast of a pointer simply returns a null pointer. Only  
failed casts of a reference result in a std::bad_cast exception.


-- 
FE


Post a reply to this message

From: Nicolas Alvarez
Subject: Re: Standard libraries
Date: 8 Mar 2009 19:56:17
Message: <49b45b20@news.povray.org>
"Fredrik Eriksson" <fe79}--at--{yahoo}--dot--{com> wrote:
> No. A failed dynamic_cast of a pointer simply returns a null pointer. Only
> failed casts of a reference result in a std::bad_cast exception.

Thanks for that. I first thought it returned null, but then I searched the
Internets and saw a try { ... } catch(std::bad_cast), so I thought I was
wrong. I didn't notice the difference was that the example I found used
references :)


Post a reply to this message

From: Nicolas Alvarez
Subject: Re: Standard libraries
Date: 8 Mar 2009 20:00:44
Message: <49b45c2b@news.povray.org>
Nicolas Alvarez wrote:
> template<typename E>
> void my_throw_exception(E& exc) {
>     //do the backtrace() dance and store the results somewhere in exc
>     throw exc;
> }

And yes, you do need the template. The 'throw' statement uses the *static*
type of the variable. So for example:

class MyError: public std::exception {};

void my_throw_exception(std::exception& exc) {
    throw exc;
}

void foo(int test) {
    switch (test) {
    case 1:
        throw std::exception();
        break;
    case 2:
        throw MyError();
        break;
    case 3:
        my_throw_exception(std::exception());
        break;
    case 4:
        my_throw_exception(MyError()); // <- MyError gets sliced here
        break;
    }
}

int main() {
    try {
        foo(1);
    } catch(MyError& e) {
        std::cerr << "MyError thrown\n";
    } catch(std::exception& e) {
        std::cerr << "std::exception thrown\n";
    }
}

It will only print "MyError thrown" if you pass 2 to foo().


Post a reply to this message

From: Darren New
Subject: Re: Standard libraries
Date: 8 Mar 2009 20:02:36
Message: <49b45c9c$1@news.povray.org>
Nicolas Alvarez wrote:
> I think the most useful feature of RTTI is not typeid() to get a string, but
> dynamic_cast.

Yeah, but I've never seen an OO language that lacks that feature. :-) 
Singling it out as something with a special name seems odd to me. I guess 
the C++ standards authors just like making up their own names for things. It 
would be like saying "Our language supports class-based OOP *and* virtual 
functions!"

Don't things like GCC support dynamic_cast and virtual functions this even 
if you compile without the switch that enables typeid? (Or with the switch 
that turns it off? I seem to be finding conflicting info, altho I might be 
looking at different compilers.)

It seems strange, too, that it would add any overhead at all, except maybe 
one word of pointer to each class's code, to point to the type_info object. 
Don't you need vtable pointers even if "RTTI" is turned off?

-- 
   Darren New, San Diego CA, USA (PST)
   My fortune cookie said, "You will soon be
   unable to read this, even at arm's length."


Post a reply to this message

From: Darren New
Subject: Re: Standard libraries
Date: 8 Mar 2009 20:06:41
Message: <49b45d91@news.povray.org>
Nicolas Alvarez wrote:
> I dunno... I think you might be able to do something like this:

I guess that would work for your own exceptions. If you get anyone else's 
library, you're kind of screwed.

I've noticed that a lot with some languages - the runtime is such that it's 
really hard to not have control of all the source code that goes into making 
up the executable.

-- 
   Darren New, San Diego CA, USA (PST)
   My fortune cookie said, "You will soon be
   unable to read this, even at arm's length."


Post a reply to this message

From: Darren New
Subject: Re: Standard libraries
Date: 8 Mar 2009 20:08:56
Message: <49b45e18$1@news.povray.org>
Darren New wrote:
> Nicolas Alvarez wrote:
>> I dunno... I think you might be able to do something like this:
> 
> I guess that would work for your own exceptions. If you get anyone 
> else's library, you're kind of screwed.

Actually, taking it one step farther, the place where it's hardest to debug 
the failure is exactly where (a) it isn't your code to change, or (b) you 
didn't expect to throw the exception (i.e., it got thrown for you). 
Otherwise, you can put enough detail into the exception manually, or just 
log the backtrace right there without dealing with exceptions at all.

-- 
   Darren New, San Diego CA, USA (PST)
   My fortune cookie said, "You will soon be
   unable to read this, even at arm's length."


Post a reply to this message

From: Darren New
Subject: Re: Standard libraries
Date: 8 Mar 2009 20:15:29
Message: <49b45fa1$1@news.povray.org>
Nicolas Alvarez wrote:
> I think the most useful feature of RTTI is not typeid() to get a string, but
> dynamic_cast.

I think my C++ fu is obviously very weak. I'm having a hard time seeing how 
boost::any works, or at least what are the requirements and syntax of the 
instantiation of it.

http://www.two-sdg.demon.co.uk/curbralan/papers/ValuedConversions.pdf

It sounds like "Employing the member template mechanism supports implicit
conversion from values of an arbitrary type into an any." means that I can 
just say

   any X = string("Hello");
and X will be an any<string> initialized to a copy of the std::string "Hello"?

-- 
   Darren New, San Diego CA, USA (PST)
   My fortune cookie said, "You will soon be
   unable to read this, even at arm's length."


Post a reply to this message

From: Warp
Subject: Re: Standard libraries
Date: 8 Mar 2009 20:47:17
Message: <49b46714@news.povray.org>
Darren New <dne### [at] sanrrcom> wrote:
> Actually, wouldn't this be clearer to do with virtual functions or pointers 
> to functions anyway? Where does typeid make things better in that scenario? 
> Isn't it just as easy to do something like

> class Alpha : Beta {
>     ....
>     virtual const char * me() { return "Alpha:Beta"; }
>     ....
> }

  It was you who complained that you have to write a "mySize()" function
in every single class derived from the base class if you wanted any useful
information about the actual object. I thought you wanted some way of
distinguishing the actual object without having to implement a function
in each class.

> Plus, you avoid the overhead of putting 
> RTTI type_info on *every* class, most of which probably won't be registered 
> that way?

  If you have a virtual function in the class or any of its parent classes,
you already have RTTI info on it. typeid() itself doesn't add anything to
the classes.

  It's also perfectly possible to ask for the typeid of a non-RTTI class
(ie. one which does not have any virtual functions). In that case you
simply get the typeid of the parameter (without the runtime checking of
whether the object is really of that type).

-- 
                                                          - Warp


Post a reply to this message

From: Darren New
Subject: Re: Standard libraries
Date: 8 Mar 2009 21:45:52
Message: <49b474d0$1@news.povray.org>
Warp wrote:
>   It was you who complained that you have to write a "mySize()" function
> in every single class derived from the base class if you wanted any useful
> information about the actual object.

You said that was a good idea, to prevent bypassing of modularity. :-)

OK, having thought about it, my criticisms in this case aren't really fair. 
It isn't reflection, but it's not called reflection, so complaining that it 
doesn't do what reflection does isn't too reasonable on my part. It would be 
nice if it did, but I understand why it doesn't.

>   If you have a virtual function in the class or any of its parent classes,
> you already have RTTI info on it. typeid() itself doesn't add anything to
> the classes.

OK, cool. That's what I thought.  (Well, it probably/possibly allocates 
space for the strings that name() returns, yes? But that's just one string 
per class, so not really a problem.)

>   It's also perfectly possible to ask for the typeid of a non-RTTI class
> (ie. one which does not have any virtual functions). In that case you
> simply get the typeid of the parameter (without the runtime checking of
> whether the object is really of that type).

Yeah. And I saw in boost::any they use it to get type information about 
things like std::string, which you can't easily add virtual functions to in 
order to support doing it yourself.

I guess it has some decent uses.

Thanks for continuing the conversation with me after I ticked you off earlier.

-- 
   Darren New, San Diego CA, USA (PST)
   My fortune cookie said, "You will soon be
   unable to read this, even at arm's length."


Post a reply to this message

From: Mike Raiford
Subject: Re: Standard libraries
Date: 9 Mar 2009 13:37:43
Message: <49b553e7$1@news.povray.org>
Tor Olav Kristensen wrote:

> 
> http://en.wikipedia.org/wiki/Kool-Aid#.22Drinking_the_Kool-Aid.22
> 

Much clearer explanation.

-- 
~Mike


Post a reply to this message

From: Darren New
Subject: Re: Standard libraries
Date: 9 Mar 2009 16:47:56
Message: <49b5807c$1@news.povray.org>
Warp wrote:
>   I surely can understand why such information could sometimes be useful,
> but it still sounds to me like you are accessing the internals of an object,
> bypassing its public interface, which is something that breaks the basic
> idea of modularity.

Here's an excellent example from Python:

http://qinsb.blogspot.com/2009/03/automatic-repr-and-eq-for-data.html

Translated for those who don't speak Python, this says "here's a mix-in 
multi-inheritance class that provides a 'show as a string' function. It 
examines the class and constructor arguments, so you can do something 
like[1] this (using C++-ish syntax):

class Alpha : Beta, ThisMixinClass {
   Alpha(int One, float Two) { ... }
}

And then call
   Alpha alpha(23, 75.2);
and then you can say
   string x = alpha.repr();
and have x get the value
   "<class Alpha One=23 Two=75.2>"


Not really breaking encapsulation at all. Just adding as a method 
functionality that you'd have to implement manually by hand in each class. 
Not really breaking modularity any more than a template that relies on a 
class having particular set of internal functions.

[1] The function __repr__() is what gets called on an instance to turn it 
into a string that when you evaluate it in Python you get back the value. In 
C++ terms, it would be like printing
    std::string("Hello")
for a std::string whose value is "Hello", rather than just Hello.
__init__() is the constructor, and there's only one and you can't overload it.


-- 
   Darren New, San Diego CA, USA (PST)
   My fortune cookie said, "You will soon be
   unable to read this, even at arm's length."


Post a reply to this message

From: Fredrik Eriksson
Subject: Re: Standard libraries
Date: 10 Mar 2009 16:01:28
Message: <op.uqldwvgj7bxctx@e6600>
On Mon, 09 Mar 2009 01:15:26 +0100, Darren New <dne### [at] sanrrcom> wrote:
>
> It sounds like "Employing the member template mechanism supports implicit
> conversion from values of an arbitrary type into an any." means that I  
> can just say
>
>    any X = string("Hello");
> and X will be an any<string> initialized to a copy of the std::string  
> "Hello"?

Yes.


-- 
FE


Post a reply to this message

<<< Previous 50 Messages Goto Initial 50 Messages

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.