The Other Worlds Shrine

Your place for discussion about RPGs, gaming, music, movies, anime, computers, sports, and any other stuff we care to talk about... 

  • I love foreach (yes this is about C++ again)

  • Somehow, we still tolerate each other. Eventually this will be the only forum left.
Somehow, we still tolerate each other. Eventually this will be the only forum left.
 #89990  by Nev
 Wed Jul 13, 2005 4:56 am
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.

 #89994  by Kupek
 Wed Jul 13, 2005 9:10 am
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>.

 #90010  by Nev
 Wed Jul 13, 2005 3:51 pm
I didn't know how to pass a brace-block into a function...what are inline function templates?

 #90028  by Kupek
 Wed Jul 13, 2005 6:13 pm
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 all
template <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.)

 #90056  by Ishamael
 Thu Jul 14, 2005 1:11 am
Ugh. I hate C++ so much. We can't move on from it fast enough...

 #90067  by Nev
 Thu Jul 14, 2005 10:45 am
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 all
template <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...

 #90073  by SineSwiper
 Thu Jul 14, 2005 12:04 pm
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 all
int *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.

 #90075  by Nev
 Thu Jul 14, 2005 12:07 pm
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.

 #90078  by Kupek
 Thu Jul 14, 2005 12:08 pm
Sine, your code examples apply to C as well; there's nothing there that is specific to C++.

 #90080  by SineSwiper
 Thu Jul 14, 2005 12:10 pm
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.

 #90082  by Nev
 Thu Jul 14, 2005 12:15 pm
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.
Last edited by Nev on Thu Jul 14, 2005 12:20 pm, edited 1 time in total.

 #90085  by SineSwiper
 Thu Jul 14, 2005 12:19 pm
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.

 #90087  by Nev
 Thu Jul 14, 2005 12:21 pm
I dunno about that. For reference, I point you at your QWERTY keyboard sitting in front of you.

 #90091  by SineSwiper
 Thu Jul 14, 2005 12:24 pm
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.