IWETHEY v. 0.3.0 | TODO
1,095 registered users | 1 active user | 0 LpH | Statistics
Login | Create New User
IWETHEY Banner

Welcome to IWETHEY!

New Interesting example, Todd
template<class T>
const T& max(const T& t1, const T& t2) { return t1 > t2 ? t1 : t2; }

I must say, I'm a bit confused by this, as what you've done by typing the parametters as const T& is using the C++ mechanism for enjoining the compiler from doing a type promotion on a parameter, then complaining that it won't do type promotions.

If you want it to do type promotions, specify the template thus:

template <class T>
const T& max (T t1, T t2)
{ return t1 > t2 ? t1 : t2; }

But wait, you'll complain, what if T is a BigAssClass type? Passing a BigAssClass object by value is not recommended, as its too big, and you don't want to load the stack with such a monstrosity. That's quite true, but then, you wouldn't want to convert a BigAssClass to an int (or float, or double, or whatever), either, would you?

[edit: used the same name for the BigAssClass type...consistency, you know...]
jb4
"We continue to live in a world where all our know-how is locked into binary files in an unknown format. If our documents are our corporate memory, Microsoft still has us all condemned to Alzheimer's."
Simon Phipps, SUN Microsystems
Expand Edited by jb4 May 6, 2003, 05:26:41 PM EDT
New Umm...
wouldn't that require that t1 and t2 be of the same type and return only that type?

(course, you could do a return of the address of the object, casted to a void pointer...then you could return either object, but that's just me).
New And what would you cast the void* back to afterwards?



"Packed like lemmings into shiny metal boxes.
Contestants in a suicidal race."
    - Synchronicity II - The Police
Expand Edited by tuberculosis Aug. 21, 2007, 06:38:46 AM EDT
New Yeah, you'd have to test for the type...
or assign it to a base class pointer that the two types are dervived from.

Of course, there are cases were you'd have to the for type for your max macro.
sprintf("%d", max(long, float));


New Aaaaaahhhhhhh!

sprintf("%d", max(long, float));


Why don't you just juggle torches while you gas up your car?

First, you're short an argument to sprintf - you forgot the destination buffer.
Second, the xxxxf family (scanf, printf, and their variants), have no place in C++.

Use strstreams.

char buffer[50];
ostrstream ost(buffer,sizeof(buffer));

ost << (max(aLong, aFloat)) << ends;



"Packed like lemmings into shiny metal boxes.
Contestants in a suicidal race."
    - Synchronicity II - The Police
Expand Edited by tuberculosis Aug. 21, 2007, 06:39:11 AM EDT
New *chuckle*
d00d, that was off the cuff. It wasn't meant to be code perfect - just to illustrate a problem with max.

Second, use stringstreams. strstreams are the old style char* stuff, iirc. You have to unfreeze any .str() you create via them...yuk.
New Oh, you illustrated several problems
max was the least of them.

Thanks for the tip on the updated streams. I can't say they're an improvement though.

Item 1: YTF did they blend istream and ostream in stringstream? Can you use it as a queue? Will it block on reads or throw an exception? We already have pipes for this stuff.

Item 2: The new version is written in terms of string. This is OK, but string does heap allocation all the time so for small buffers its likely to suck compared to stack memory. You'll note that in my code I don't need to call str or freeze as I already have the buffer. So it looks like streaming to a known memory location is no longer supported. So much for high performance code.

Item 3: You can construct one with no buffer (it'll use a string) or a const string&. Nifty. Suppose I want to stream into my own string. I'll miss WriteStream on: myString.

WTF are this people thinking?



"Packed like lemmings into shiny metal boxes.
Contestants in a suicidal race."
    - Synchronicity II - The Police
Expand Edited by tuberculosis Aug. 21, 2007, 06:39:47 AM EDT
New Your code should crash
returning a reference to a temporary stack variable like that. I'm assuming this was a copy paste error - its one that can crash your program with a memory access violation though (if you're lucky).

Tell me again about the safety of the language and how it keeps you from screwing yourself.

I must say, I'm a bit confused by this, as what you've done by typing the parametters as const T& is using the C++ mechanism for enjoining the compiler from doing a type promotion on a parameter, then complaining that it won't do type promotions.


Funny, I thought I was employing the C++ mechanism for reducing stack growth.

But wait, you'll complain, what if T is a BigAssClass type? Passing a BigAssType object by value is not recommended, as its too big, and you don't want to load the stack with such a monstrosity. That's quite true, but then, you wouldn't want to convert a BigAssObject to an int (or float, or double, or whatever), either, would you?


Really? I can imagine all kinds of scenarios of BigAssOrderedType. Here's a good one - suppose I have 2DVector and 3DVector. It makes sense that I can promote a 2Dvector to a 3Dvector - and if I mixed them, I'd certainly like promotion to happen towards 3Dvector. So maybe I implement operator 3Dvector on 2Dvector (being careful not to implement operator 2Dvector on 3Dvector lest we end up in ambiguity hell).

Boy, we've sure thought a lot about types without giving what the heck we're supposed to be doing a lick of thought. Thats why I think C++ is unredeemable shit. Working the type system consumes all available cycles and often - you still don't have an ironclad solution (like here).

The bottom line in this scenario is clearly, passing classes by value is just not worth the trouble it causes and the feature ought not to be there at all. Yes C allows you to pass structs by value - no that wasn't a good idea.

The smart thing to do would be to simply ignore the feature like Objective C did (objects only live on the heap - object references can live on the stack). The attempt to unify classes and structs was, in retrospect, a really bad choice.



"Packed like lemmings into shiny metal boxes.
Contestants in a suicidal race."
    - Synchronicity II - The Police
Expand Edited by tuberculosis Aug. 21, 2007, 06:38:33 AM EDT
New My code does crash, but not as often as you think.
First, if you follow my posts, you'll know that my typing skills (such as they are) are nothing short of abyssmal. ;-)

I must say, I'm a bit confused by this, as what you've done by typing the parametters as const T& is using the C++ mechanism for enjoining the compiler from doing a type promotion on a parameter, then complaining that it won't do type promotions.

Funny, I thought I was employing the C++ mechanism for reducing stack growth.

To a degree, that's true. But you have also enjoined the compiler from doing the standard promotions. Which is OK, but if you're going to do that, you need to be aware of the side effects. (I understand that Perl has similar syntatical quirks that will screw up the illiterati if they're not fully grokked.)

Boy, we've sure thought a lot about types without giving what the heck we're supposed to be doing a lick of thought.

I disagree. We have been spending a lot of time decising the kinds of operations we need to do on a BigAssClass, or a BigAssOrderedType. Your preceding paragraph specified what we want to be able to do. Now, given the tools available to do that, we start planning how to do it. If my tool set includes C++ , then I worry about how to manipulate the types to permit me to get where I want. If I have access to Python, or Objective C, then a whole different set of manipulations will have to occur. (If all I have is C# or Visual Basic, please put me out of my misery ASAP....) I will freely admit that the implementational issues that C++ presents are...unique...and and not always straightforward. But you will have a tough time convincing me that these other languages bandied about in this thread as panaceas to all the ills of programming ever foisted upon mankind are without their own set of problems, peculiarities, and pecadilloes.



jb4
"We continue to live in a world where all our know-how is locked into binary files in an unknown format. If our documents are our corporate memory, Microsoft still has us all condemned to Alzheimer's."
Simon Phipps, SUN Microsystems
New Re: My code does crash, but not as often as you think.
But you will have a tough time convincing me that these other languages bandied about in this thread as panaceas to all the ills of programming ever foisted upon mankind are without their own set of problems, peculiarities, and pecadilloes.

Answer #1: especially if you never try them out.
Answer #2: I don't think anyone is trying to convince you of that. What we are trying to convince you of is the fact that C++ has serious problems, much more so than other languages.

Still waiting for you to respond to the two large posts that Todd and I put together for you, BTW.
Regards,

-scott anderson

"Welcome to Rivendell, Mr. Anderson..."
New Replies coming
Posts read, and comments pending. Hard to find a suitable time to respond in full under current workload (esp considering my typing skilz...;-) )
jb4
"We continue to live in a world where all our know-how is locked into binary files in an unknown format. If our documents are our corporate memory, Microsoft still has us all condemned to Alzheimer's."
Simon Phipps, SUN Microsystems
New Re: My code does crash, but not as often as you think.

We have been spending a lot of time decising the kinds of operations we need to do on a BigAssClass, or a BigAssOrderedType. Your preceding paragraph specified what we want to be able to do. Now, given the tools available to do that, we start planning how to do it.


Well, unfortunately, when you define that max function in terms of T, you know have to keep in the back of your mind the potential effects of calling max(SomethingYouDidntReallyThinkThatHardAbout,SomethingElseYouDidntReallyThinkThatHardAbout);

IOW, that template mechanism paints with an infinitely broad brush and you now have to understand its exact expansion, along with expansions of any possible specializations you have (or your team mate wrote and didn't tell you about), and any automatic type conversion operators you've written along with any single argument constructurs you have.

To illustrate this, allow me to tell you about a short little gig I had as a porting engineer. During a development cycle when everybody was working on windows (except me, 'cause I just don't do windows), I would check out the code every morning and spend hours trying to build it - fixing portability issues and sending out hate mail to the team about some of their stupid assumptions.

One day I hit a compile error on something like:

CustomString str = "foo";
... return foo + "bar";

I looked up the developer and showed him the header for CustomString (this is before stl compiled anywhere other than Borland btw) and pointed out that there was no operator+ implemented on CustomString. He seemed perplexed because it worked fine on Windows. And so it did. As CustomString had an operator const char* defined, the MSString object had a ctor for const char*, an operator+, and an operator const char*. As we traced the execution we found that the compiler on windows had figured out it could construct a temp MSString (whatever they call it, I forget), do the operator+, then construct CustomString temp off the MSString's operator const char*.

Something like that - but the type conversions/automatic operator calls were several layers deep and of course there is no MSString for unix.

This lead me to conclude that the C++ type system is too complex for any human to predict what will happen for a given bit of code, and the introduction of a single function linking a couple types together and have far reaching unintended consequences on the system's overall behavior.

Thus, I boldly make the statement "C++ doesn't scale".



"Packed like lemmings into shiny metal boxes.
Contestants in a suicidal race."
    - Synchronicity II - The Police
Expand Edited by tuberculosis Aug. 21, 2007, 06:39:07 AM EDT
     C++ File I/O: Windows vs. Linux - (tjsinclair) - (85)
         Wild guess - (JayMehaffey) - (1)
             Nope - (tjsinclair)
         Permissions problem? - (tuberculosis) - (16)
             Possibly - (tjsinclair) - (15)
                 The only dumb question is... - (jbrabeck) - (14)
                     Thanks - (tjsinclair) - (13)
                         I'd second the permissions... - (jbrabeck) - (12)
                             Played with error bits - (tjsinclair) - (11)
                                 C++ for real work - (tuberculosis) - (10)
                                     telco's use lots of c++ -NT - (boxley) - (7)
                                         on UNIX? - (tjsinclair) - (6)
                                             well several softswitches and other software (added link) - (boxley) - (2)
                                                 Yeah - OS level code I think - (tuberculosis) - (1)
                                                     Stacked based types? - (deSitter)
                                             some more info on c++ Unix and Telcos and jobs - (boxley) - (2)
                                                 CORBA == Objects, so makes sense - (tjsinclair)
                                                 Resume mailed -NT - (tuberculosis)
                                     Someone somewhere must be using it - (tjsinclair)
                                     Network Management software - (Arkadiy)
         I don't know C++ but... - (ben_tilly) - (6)
             Seems like it ought to raise an exception on failure -NT - (tuberculosis) - (5)
                 It does - (tjsinclair) - (4)
                     Erroring out? - (tuberculosis) - (3)
                         I checked the ios error flags - (tjsinclair) - (2)
                             Thats not raising an exception - (tuberculosis) - (1)
                                 Agreed - (tjsinclair)
         Got it! - (tjsinclair) - (58)
             Now, this is really scary - (Arkadiy) - (53)
                 Even scarier - (tjsinclair) - (52)
                     At this point, one wants to,, - (deSitter) - (3)
                         Ross, do we need a refresher... - (jb4) - (2)
                             I would completely agree with you except - (tjsinclair) - (1)
                                 Wow! - (jb4)
                     Which version of gcc? - (admin)
                     So, let me get this straight... - (Arkadiy) - (46)
                         Something like that, yes - (tjsinclair) - (44)
                             I've been trying to do that for years - (tuberculosis)
                             Barney is not responsible... - (jb4) - (42)
                                 Move farther up the food chain - (tuberculosis) - (41)
                                     OK, I'll bite... - (jb4) - (39)
                                         Errr... - (admin) - (34)
                                             I've heard strong typing called several different things... - (jb4) - (33)
                                                 Strict typing is like training wheels. - (admin) - (32)
                                                     Re: Strict typing is like training wheels. Or Helmuts? - (jb4) - (31)
                                                         You have much to learn - (tuberculosis) - (23)
                                                             Irony - (ben_tilly) - (22)
                                                                 Additionally... - (admin) - (21)
                                                                     That's the C++ propaganda - (tuberculosis) - (20)
                                                                         Wrong usage - (Arkadiy) - (7)
                                                                             Return value problem - (tuberculosis) - (6)
                                                                                 I'll take a look for the solution for ret value - (Arkadiy)
                                                                                 Re: Return value problem - (deSitter) - (4)
                                                                                     In the end - (Arkadiy) - (3)
                                                                                         A better statically typed language - have you tried Haskell? -NT - (ben_tilly) - (1)
                                                                                             Read the tutorials - (tuberculosis)
                                                                                         Today's link at /. - (Arkadiy)
                                                                         Interesting example, Todd - (jb4) - (11)
                                                                             Umm... - (Simon_Jester) - (5)
                                                                                 And what would you cast the void* back to afterwards? -NT - (tuberculosis) - (4)
                                                                                     Yeah, you'd have to test for the type... - (Simon_Jester) - (3)
                                                                                         Aaaaaahhhhhhh! - (tuberculosis) - (2)
                                                                                             *chuckle* - (Simon_Jester) - (1)
                                                                                                 Oh, you illustrated several problems - (tuberculosis)
                                                                             Your code should crash - (tuberculosis) - (4)
                                                                                 My code does crash, but not as often as you think. - (jb4) - (3)
                                                                                     Re: My code does crash, but not as often as you think. - (admin) - (1)
                                                                                         Replies coming - (jb4)
                                                                                     Re: My code does crash, but not as often as you think. - (tuberculosis)
                                                         Finally, something other than hand-waving. - (admin) - (6)
                                                             Hey I know Java - (tuberculosis) - (3)
                                                                 Whoops, forgot that one. - (admin) - (2)
                                                                     I always sit on a towel when using it - (tuberculosis) - (1)
                                                                         Interesting... I find I get more use out of... - (admin)
                                                             I've done it - (Arkadiy) - (1)
                                                                 Re: I've done it - (admin)
                                         How? - (tuberculosis) - (3)
                                             Erm? - (jb4) - (2)
                                                 GNU CPP version 2.95.2 19991024 (release) -NT - (tuberculosis) - (1)
                                                     Sorry to hear that...:-( - (jb4)
                                     Well said, exactly correct -NT - (deSitter)
                         This is frigging runtime lib bug, not language - (Arkadiy)
             You were mis-using the flags... - (Simon_Jester) - (3)
                 I thought of that - (tjsinclair) - (2)
                     For what it's worth, code also runs... - (a6l6e6x) - (1)
                         Figured that -NT - (tjsinclair)

But if you draw a bow, draw the STRONG-est! YEEEEESSS! And if you use an arrow, use the LONGEST! OH YES!!
244 ms