Page 1 of 1
I love foreach (yes this is about C++ again)
PostPosted:Wed Jul 13, 2005 4:56 am
by Nev
I LOVE foreach. I did a macro implementation of it in C++ for my abstract container class and it makes code size so small...brings tear to my eye...
I really ought to look at putting the STL in there and seeing if it's worth it, but my container's already working well enough to do what we need it to.
PostPosted:Wed Jul 13, 2005 9:10 am
by Kupek
Why use a macro? An inline template function serves the same purpose, but it allows type checking and avoids unwanted side effects. Take a look at <TT>std::for_each</TT>.
PostPosted:Wed Jul 13, 2005 3:51 pm
by Nev
I didn't know how to pass a brace-block into a function...what are inline function templates?
PostPosted:Wed Jul 13, 2005 6:13 pm
by Kupek
Mental wrote:I didn't know how to pass a brace-block into a function...what are inline function templates?
That's basically a closure, which is not supported in C++. But you can get the same effect using function objects. An inline function template is a function that takes template parameters and is inlined at compile time so there's no function call overhead.
<tt>std::for_each</tt> is generally implemented as
Code: Select alltemplate <class Iterator, class Function>
inline for_each(Iterator first, Iterator last, Function f)
{
for ( ; first != last; ++first)
f(*first);
return f;
}
This applies <TT>f</TT> to each element in the sequence defined by [<tt>first</tt>, <TT>last</TT>). Since the type of iterator is a template parameter, this function works on native arrays (where the iterators are simply pointers) or on containers (where the iterators are objects with <TT>*</TT>, <TT>!=</TT>, and pre-<TT>++</TT> defined). Further, since the type of the function is a paremeter, it works on any function reference of a function object. (A function object is an object which has the application operator, <TT>()</TT>, defined.)
PostPosted:Thu Jul 14, 2005 1:11 am
by Ishamael
Ugh. I hate C++ so much. We can't move on from it fast enough...
PostPosted:Thu Jul 14, 2005 10:45 am
by Nev
Kupek wrote:Mental wrote:I didn't know how to pass a brace-block into a function...what are inline function templates?
That's basically a closure, which is not supported in C++. But you can get the same effect using function objects. An inline function template is a function that takes template parameters and is inlined at compile time so there's no function call overhead.
<tt>std::for_each</tt> is generally implemented as
Code: Select alltemplate <class Iterator, class Function>
inline for_each(Iterator first, Iterator last, Function f)
{
for ( ; first != last; ++first)
f(*first);
return f;
}
This applies <TT>f</TT> to each element in the sequence defined by [<tt>first</tt>, <TT>last</TT>). Since the type of iterator is a template parameter, this function works on native arrays (where the iterators are simply pointers) or on containers (where the iterators are objects with <TT>*</TT>, <TT>!=</TT>, and pre-<TT>++</TT> defined). Further, since the type of the function is a paremeter, it works on any function reference of a function object. (A function object is an object which has the application operator, <TT>()</TT>, defined.)
If I could think over the three hours sleep loss I just got because some douchebag has decided brutally loud construction outside my apartment starts at seven every morning, I'd respond. I'd like to look at this later when my mind isn't filled with bloodlust at asshole construction site owners...GOD...I hate this guy, whoever he is...
PostPosted:Thu Jul 14, 2005 12:04 pm
by SineSwiper
Ishamael wrote:Ugh. I hate C++ so much. We can't move on from it fast enough...
Aye. The language isn't COBOL, but it is still a royal fucking mess. Just take pointers and references for example:
Code: Select allint *q; /* This is a pointer of an integer, with that stupid space between int and * */
q = &x; /* This is a reference. */
x = *q; /* And this is a dereference?! What fucking moron made this shit up backwards?! If "int *q" is a pointer declaration,
shouldn't "x = *q" be putting a pointer into x?! */
*x = *y; /* WHAT?!? Are these pointers or dereferences?!?! */
**x = **y; /* And this is a pointer of a pointer or a dereference of a pointer?! */
Jesus, the whole point of C is using pointers and they royally fuck that up. That alone is worth declaring the whole language to be a piece of shit, as every other language does this 10x better.
PostPosted:Thu Jul 14, 2005 12:07 pm
by Nev
Just because some of the syntax is unwieldy doesn't mean the whole language is crap. There are a number of very good things about it, and I think you're bitching too much.
Besides which, what you're really talking about is C, not C++. All the pointer syntax is inherited from C.
PostPosted:Thu Jul 14, 2005 12:08 pm
by Kupek
Sine, your code examples apply to C as well; there's nothing there that is specific to C++.
PostPosted:Thu Jul 14, 2005 12:10 pm
by SineSwiper
Exactly, which means both languages have the same fault. I was talking about both of them. They really could have designed a better low-level language.
PostPosted:Thu Jul 14, 2005 12:15 pm
by Nev
I'm not saying I think it's great by modern standards, but given that as far as I know it was developed close to thirty years ago, I'll cut the designer(s) a little slack.
EDIT: "The initial development of C occurred at AT&T Bell Labs between 1969 and 1973; according to Ritchie, the most creative period occurred in 1972. It was named "C" because many of its features were derived from an earlier language called "B". Accounts differ regarding the origins of the name "B": Ken Thompson credits the BCPL programming language, but he had also created a language called Bon in honor of his wife Bonnie." From the Wikipedia. Seems to be closer to forty years.
PostPosted:Thu Jul 14, 2005 12:19 pm
by SineSwiper
I know. I just think we can design a new one, but alas C/C++ is too entrenched in the programmers' world to bother with a new design. I imagine when parallel programming takes off, we'll have to design something new to make it more managable.
PostPosted:Thu Jul 14, 2005 12:21 pm
by Nev
I dunno about that. For reference, I point you at your QWERTY keyboard sitting in front of you.
PostPosted:Thu Jul 14, 2005 12:24 pm
by SineSwiper
Mental wrote:I dunno about that. For reference, I point you at your QWERTY keyboard sitting in front of you.
Yeah, I know. I tried Dvorak, but it's hard to untrain 25 years of typing.