Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

C++

Name: Anonymous 2008-09-17 13:47

1) Why do people not like C++?
2) Is C++ a good language to learn programming? Mind you, you don't need to use any of the advanced features.

Name: Anonymous 2008-09-24 3:47

>>1
Read http://yosefk.com/c++fqa/ - it will answer all your questions (and raise some new ones).

Name: Anonymous 2008-09-24 4:42

>>40
the compiler can't optimize it out because Iter  operator++ (int); might do something completely different than Iter& operator++ ();,

yes, c++ really is that broken.

Name: Anonymous 2008-09-24 6:10

>>40
Yes, compiler optimizes it in case of ints, pointers, etc. So you can write i++; or ++i; And we were used to that in C.
But in C++ you do it to iterators, and suddenly
for(vector<int> iterator it = v.begin(); it != v.end(); it++)
is worse than
for(vector<int> iterator it = v.begin(); it != v.end(); ++it)

because, as >>42 said, compiler doesn't know if ++it works exactly like it++, so it creates a NEW FUCKING COPY (then destroys it) of the iterator EVERY TIME it's incremented.

All because for is such a low-level construction.

A little foreach construct wouldn't hurt. But it certainly won't solve all of C++'s problems, and knowing it, will introduce some more.

Name: Anonymous 2008-09-24 6:15

bawwww C++ isn't like my high level languages!

Name: Anonymous 2008-09-24 6:35

>>44
idiot

Name: Anonymous 2008-09-24 7:06

>>43
C++ does have for_each in <algorithm>

Name: Anonymous 2008-09-24 7:21

>>46
for_each is pretty useless without closures.

Name: Anonymous 2008-09-24 7:33

>>47
You seem to forget that C++ has functors. Function objects.

Name: Anonymous 2008-09-24 7:33

Name: Anonymous 2008-09-24 7:49

>>49
boost::lambda is an ugly abuse of C++ mechanisms. Well, almost everything is, but lambda especially so.

To quote Yossi:

Check out the monstrous boost lambda library designed to work around the lack of closures in C++ in a desperate attempt to make higher-level functions of the kind defined at <algorithm> not entirely useless. When I tried it, gcc wouldn't compile it without -ftemplate-depth=40  (the default template nesting depth limit, 17, is not enough for this library), and I got 5 screens of error messages from a single line of code using the thing. See also this thread (http://groups.google.com/group/comp.lang.c++.moderated/browse_thread/thread/100dd325c6d9ef77/a4444dc5dc9d94c0), especially the part where they explain how cout << _1 << endl works but cout << "\t" << _1 << endl doesn't (to get there quick, search for "fails miserably", then continue to the reply).

Name: Anonymous 2008-09-24 7:52

>>50
gcc
Well there's your problem. Try a better compiler, like Microsoft's cl.exe

Name: Anonymous 2008-09-24 8:22

>>48
That's kind of like saying that Java has closures because it has anonymous classes.

Name: Anonymous 2008-09-24 8:41

>>52
And many a Java apologist has said just that, God bless their souls!

Name: Anonymous 2008-09-24 9:04

>>50
Admittedly. I always used to use boost::bind over lambda, simply because it wasn't as fucking insane. Bind lets you hand curry functions which is almost as good as a closure. It's still a fucking mess of hacks, but what from Boost isn't? Dealing with shit like that is your punishment for using the broken language Sepples in the first place.

>>52
Not really. Functions are 1st-class types in C++, ie, int(*main)(int,char*[]), or the slightly more dubious int(*make_counter(int))() which is the signature of a function which takes an int and returns a pointer to a function which takes no parameters and returns an int.

The problem with using raw function pointers in C/C++ is that there's no real way to attach state to them without doing it by hand (with a function object). But they are still first-class citizens, which I'd argue is completely different from Java's anonymous class hack (because Java makes me weep at night).

Name: Anonymous 2008-09-24 9:38

>>54
Function objects in C++ and anonymous classes in Java are pretty much the same thing. You have an object with some state and a single method. They only difference is that with anonymous classes you need to explicitly call a method, whereas a function object looks like an ordinary method call because of operator overloading.

Name: Anonymous 2008-09-24 10:38

>>55
I want to argue with you, wrote out a long post and everything, then the light came on and realized that you're more right than I am. The part I got caught up on was the functor creation semantics -- C++ has function pointers which let you easily reference functions, whereas in Java you have to wrapper the function call into an anonymous class before you can do anything with it.

Both methods do the exact same thing, as you said, except the Java way of doing it is excessively more verbose.

Name: Anonymous 2008-09-24 11:00

too bad java is SLOW AS FUCK

Name: Anonymous 2008-09-24 11:33

Java 7 will have closures.

Name: Anonymous 2008-09-24 12:04

>>58
>>52
But java inner classes ARE closures.  All object-instance variables are available to the inner class methods.  And for anonymous inner classes, even the function body's final variables are available.

Unless you have some faggot definition of "closure" that specifically only includes your language of choice.

Name: Anonymous 2008-09-24 12:38

even the function body's final variables are available
That's not a closure.

Name: Anonymous 2008-09-24 12:50

>>59
object-instance variables are available to the inner class methods
That's not a closure either, it's a scoping benefit. The idea behind a closure is that the environment is captured and persists, such that it can be called later. Now, I'm not a Java person, so feel free to clarify me on this, but given the following code --

class Outer {
    int m_var;
    class Inner {
        int get() {
            return m_var;
        }
    }
    Inner get_inner() {
        return new Inner();
    }
}

Outer o = new Outer();
o.m_var = 1;
Inner i1 = o.get_inner();
o.m_var = 2;
Inner i2 = o.get_inner();


You're saying that the value of i1.get() == 1 and i2.get() == 2, correct? I was under the impression that Java did not save the environment (and thus i1.get() == i2.get() == 2) in which case it isn't a closure.

I don't have a java compiler installed. How does the (syntactically correct version of this) code behave?

Name: Anonymous 2008-09-24 13:10

>>61

class Outer
{
        private int var;

        class Inner
        {
                int get() { return var; }
        }

        Inner getInner() { return new Inner(); }

        void setVar(int v) { var = v; }

        public static void main(String[] args)
        {
                Outer o = new Outer();
                o.setVar(1);
                Inner i1 = o.getInner();
                o.setVar(2);
                Inner i2 = o.getInner();
                if(i1.get() == 1 && i2.get() == 2)
                        System.out.println("Worked!");
                else
                        System.out.println("Failed!");
        }
}


Yep, this code fails. There's no use debating this: Java doesn't have closures, just a hack that can emulate closures in certain situations. Saying Java has closures because it has inner classes is like saying C has first-class functions because it has function pointers.

Name: Anonymous 2008-09-24 13:32

(define var 1)

(define (inner)
    (lambda () var))

(define i1 (inner))

(set! var 2)

(define i2 (inner))


> (i1)
2
> (i2)
2

Name: Anonymous 2008-09-24 13:37

>>63
(set! var 2)
Now you have two problems.

Name: Anonymous 2008-09-24 13:40

Question: if I x <- 2 then y <- 3+x, then x <- 3, will y be 5 or 6 (in SCHEME)?

Name: Anonymous 2008-09-24 13:50

>>65
5

Name: Anonymous 2008-09-24 13:55

Thats gay.

Name: Anonymous 2008-09-24 13:58

>>61
>The idea behind a closure is that the environment is captured and persists
Yeah, and this code fails just like >>64, because the same environment is captured two times. To make it work, you'd have to create two objects - which IMO is analogous to calling a function returning closure twice, for example

(define (plus x)
   (lambda (y) (+ x y)))

(define plus3 (plus 3))
(define plus2 (plus 2))


In this way, closures are analogous to objects.

Name: Anonymous 2008-09-24 14:15

you are all gay, I'm going to fuck you up

Name: Anonymous 2008-09-24 14:42

JAVA DOES HAVE CLOSURES

final int[] env = {31337};
Runnable closure = new Runnable() {
    @Override
    public void run() {
        System.out.println("env was " + env[0]);
        env[0] = 31338;
        System.out.println("env is now " + env[0]);
    }
};

closure.run();


env was 31337
env is now 31338

Name: Anonymous 2008-09-24 15:05

>>70
2/10, what the hell.

Name: Anonymous 2008-09-24 17:01

>>70
He's right, you know.

Name: Anonymous 2008-09-24 17:05

Closures are just a hack for currying.

Name: Anonymous 2008-09-24 17:16

>>73
currying is a hack for closures

Name: Anonymous 2008-09-24 18:25

>>70
Shitty, convoluted ones, and regular methods and classes aren't. This is so half-assed it's not even funny, and it arrived so late  nobody's ever going to use it because all Javatards doing Java for Javatarded companies know is Java 1.0.

Name: Anonymous 2008-09-24 19:13

>>75
>>70 didn't arrive late; it's always been part of the spec.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List