Weird statement in osc/saw sync


#1

I was studying the bandlimiting implementation of axoloti objects when i stumbled in something curious:
In k-rate code of osc/saw sync, at row 15 there's something like this:

vgain[nextvoice] = vgain[nextvoice] = (((x*(freq>> .. etcetera ;

Is that some kind of special statement in c++ or is it just a typo?


#2

I cannot find it there. Sounds like accidently pasted, maybe try a sync?


#3

whoops, i made an error in the topic! The code was from osc/saw sync
You can see it on github too: https://github.com/axoloti/axoloti-factory/blob/1.0.12/objects/osc/saw%20sync.axo

Usually i'm very careful when working with factory objects, i always embed them as patch/object.

The weird statement is still there


#4

its a typo...

its perfectly valid C syntax, an assignment operator returns value assigned.
so you can do something like

a = b = c>> 4;

but obviously in osc/saw case it does nothing.... (and the optimiser will strip it out :wink:)


#5

Odd syntax...
Would a, b and c all end up with the same value, then?

a|x


#6

yes...

but its actually a bit more interesting than that in C++ :slight_smile:

take the following:

    int a = 4;
    int b = 3;
    b=(a=0)++;

what is the value of a & b?
... and for bonus points, why?
... and extra bonus points, why was it not the same in C? (assume K&R)

Im capable of giving evil C++ questions to interviewees :smiling_imp:

and, yeah, while the above is not particularly useful, there are quite a lot of situations where it can be very useful.


#7

Let me guess.. a = b = 0

At first a is set to 0, then b is set to a=0, and then 0++ is computed, but not actually stored anywhere.

I'd be happy to be wrong, though! (Because i never actually studied C or C++, so i wish i could learn something new)


#8

nope, a=1, b=0 :slight_smile: - (most will usually assume/guess, a=0, b=1 :wink:)

basically its because the assignment operator actually returns a reference, not an lvalue.

which means you can also do amusing things like this
(A=5)++;
which will set A to 6
(now to be clear this is because the object A is returned from the assignment operator, which means the ++ works on it, this is i.e. its not being treated as A=(5++), so (A=5)=4; is also valid syntax)

in fairness, this stuff never gets taught (nor is in books)... its something you tend to find out if you 'play' with language syntax...
in this particular case, this should become 'evident' when you learn to overload the assignment operator, you should really use

class A {
public:
    A& operator=(const A&);
}

see how it returns a reference... so once you know this, its logical (I think) to ask yourself the question... does this apply to built in types (and the answer is yes, hence all the above syntax)
... the reasoning, is of course its more efficient to return a reference, than to start copying values around.
(in K&R C it was an r-value, so you could not do this, not really so evident, as you cant overload types in C :wink:)