• Member Since 11th Jul, 2011
  • offline last seen 14 minutes ago

kits


All my things are horse things!

More Blog Posts226

  • 32 weeks
    Available!

    For the first time in a decade, you can buy a copy of Twilight's List again! And it's got a softcover option too! And and since it's Lulu, if you aren't in North America, the shipping won't kill you! I'm celebrating!

    Read More

    2 comments · 308 views
  • 41 weeks
    Small Writing Thing

    I participated in another group writing project. You can find it Over here.

    I was feeling very cross at the government when I gave Rainbow a metal pipe. I actually love her here though.

    Clang.

    5 comments · 116 views
  • 46 weeks
    June

    3 comments · 243 views
  • 51 weeks
    New (again) Developments

    Hi!
    It's been a while. But I wanted to let people who were asking (and everyone else) know a neat thing!

    Read More

    5 comments · 491 views
  • 71 weeks
    A thing to read

    Hi!

    I commissioned a short fic from Monochromatic. You can read it over on Hollow Shades.

    It’s rarity and rainbow and twilight being pals.

    4 comments · 189 views
Sep
27th
2013

C++1y · 9:51pm Sep 27th, 2013

Oh god. The fools. What have they allowed me to wrought?

#include <functional>
#include <iostream>
#include <string>
using namespace std;
#define defn(name,sig) function< sig > name
struct s {
int i=0;
defn(func, string() ) = [&](){ return string("hello"); };
};
struct d : public s {};
struct f : public s {
int i=1;
defn(func, string() ) = [&](){ return s::func() + " world"; };
};
int main() {
d D;
cout<<s().func()<<endl;
cout<<D.func()<<endl;
cout<<f().func()<<endl;
D.func = [D](){ return D.func()+ " goodbye"; };
cout<<D.func()<<endl;
}

Does this bring prototyped objects into the realm of c++?

$~g++-mp-4.9 --std=c++1y test.cpp 2>&1 && ./a.out
hello
hello
hello world
hello goodbye
6

Yes
D:
Yes it does.
;_;

I just felt the need to share the horror

Report kits · 1,183 views ·
Comments ( 44 )

:rainbowderp: What in the name of all that is holy was that? I assume it had something to do with programming and all that jazz. *Sigh, if only I could do anything like that.

~SolidFire

People should just use C#, if only for the nicer appearance of the code snippets.

I'm all Java over here.

I feel bad, because I've studied computer science for three years now and I don't understand exactly what you're getting at. I've learned Java, C# and a bit of C, but I haven't gotten to C++ yet - and I didn't know what a prototyped object was until I looked at wikipedia just now.

Never mind me, just a noob at java pretending I understand what the heck this is. :twilightsheepish:

1379770
>C#
>ever
If I'm giving up performance, I want it to at least run on things that aren't windows.

1379783
Good luck. C++ is... special. And rapidly changing. C++2011 was a massive update. 14 is a smaller one, but 17 is going to be big apparently.

At least they're working the kinks out of auto, even if they specifically disallow it in the context that would clean the syntax up here a bit

1379770

No. If nice looking code is the goal then Ruby should be used, as it is at least portable.

And Mono does not really allow C# to be ported, there are too many 'slight' difference between it and MS. And then there are the platform issues from specific platforms that C# was never designed to deal with...

So no.


As to the blog, could be useful once compilers supporting it are stable.

I suppose they added it without requiring any 'weirdness'* in using types that make use of this feature?

* In D it stuffs a 'default object' of each type into the binary to memcpy when default constructing a new object. And then insists on reverting to said default after move-copying...

1379798
Python FTW! :rainbowwild:

I used to know some C++ a few years ago, but I seriously have no idea what that is you're doing. I guess I haven't been keeping up with the standards.

1379798
Well, llvm just posted about being 14 draft complete. gcc is 1/2 way there. MSVC isn't even done with 11.

As for weirdness...
lambdas are implemented as unnamed functors (objects that overload operator() ). std::function holds either a function pointer or a functor/lambda. I think it copies it, but am not sure. the [D] part DOES make a copy of D and pass it into the scope of the lambda. So it is now independent of the original D. I assume one have passed in a copy of D.func and not copied all of D. (Copies are made by the implicit d(const d&)). I could have MOVED it too (maybe. Not sure if lambdas are moveable).
C++'s default copy ctor copies all the members from the to-copy object. I have no idea what happens to one of these after it's moved from, but since using a moved-from object is undefined behavior, it doesn't matter really.

This is semantically equivalent to something like

struct f {
using func_type = std::string (*) ();
func_type func = &someFunc;
} F;
...
std::cout<<*(F.func)()<<std::endl; //calls someFunc() via pointer
F.func=&someOtherFunc;
std::cout<<*(F.func)()<<std::endl; // calls someOtherFunc() via pointer

but it LOOKS less like you're playing function pointer games and MORE like inheritance. Thus, it's evil.

1379814
It's changed massively.
And this is peanuts. I have a variadic argument zip function that takes any number of sequences of the same length and returns a sequence of tuples. All typesafe. Doesn't sound like much for something like python, but in C++ it's pretty much insane.

Here's a Cartesian product. Lol

The Story of Mel
by Ed Nather

A recent article devoted to the macho side of programming
made the bald and unvarnished statement:

Real Programmers write in FORTRAN.

Maybe they do now,
in this decadent era of
Lite beer, hand calculators, and “user-friendly” software
but back in the Good Old Days,
when the term “software” sounded funny
and Real Computers were made out of drums and vacuum tubes,
Real Programmers wrote in machine code.
Not FORTRAN. Not RATFOR. Not, even, assembly language.
Machine Code.
Raw, unadorned, inscrutable hexadecimal numbers.
Directly.

Lest a whole new generation of programmers
grow up in ignorance of this glorious past,
I feel duty-bound to describe,
as best I can through the generation gap,
how a Real Programmer wrote code.
I'll call him Mel,
because that was his name.

I first met Mel when I went to work for Royal McBee Computer Corp.,
a now-defunct subsidiary of the typewriter company.
The firm manufactured the LGP-30,
a small, cheap (by the standards of the day)
drum-memory computer,
and had just started to manufacture
the RPC-4000, a much-improved,
bigger, better, faster — drum-memory computer.
Cores cost too much,
and weren't here to stay, anyway.
(That's why you haven't heard of the company,
or the computer.)

I had been hired to write a FORTRAN compiler
for this new marvel and Mel was my guide to its wonders.
Mel didn't approve of compilers.

“If a program can't rewrite its own code”,
he asked, “what good is it?”

Mel had written,
in hexadecimal,
the most popular computer program the company owned.
It ran on the LGP-30
and played blackjack with potential customers
at computer shows.
Its effect was always dramatic.
The LGP-30 booth was packed at every show,
and the IBM salesmen stood around
talking to each other.
Whether or not this actually sold computers
was a question we never discussed.

Mel's job was to re-write
the blackjack program for the RPC-4000.
(Port? What does that mean?)
The new computer had a one-plus-one
addressing scheme,
in which each machine instruction,
in addition to the operation code
and the address of the needed operand,
had a second address that indicated where, on the revolving drum,
the next instruction was located.

In modern parlance,
every single instruction was followed by a GO TO!
Put that in Pascal's pipe and smoke it.

Mel loved the RPC-4000
because he could optimize his code:
that is, locate instructions on the drum
so that just as one finished its job,
the next would be just arriving at the “read head”
and available for immediate execution.
There was a program to do that job,
an “optimizing assembler”,
but Mel refused to use it.

“You never know where it's going to put things”,
he explained, “so you'd have to use separate constants”.

It was a long time before I understood that remark.
Since Mel knew the numerical value
of every operation code,
and assigned his own drum addresses,
every instruction he wrote could also be considered
a numerical constant.
He could pick up an earlier “add” instruction, say,
and multiply by it,
if it had the right numeric value.
His code was not easy for someone else to modify.

I compared Mel's hand-optimized programs
with the same code massaged by the optimizing assembler program,
and Mel's always ran faster.
That was because the “top-down” method of program design
hadn't been invented yet,
and Mel wouldn't have used it anyway.
He wrote the innermost parts of his program loops first,
so they would get first choice
of the optimum address locations on the drum.
The optimizing assembler wasn't smart enough to do it that way.

Mel never wrote time-delay loops, either,
even when the balky Flexowriter
required a delay between output characters to work right.
He just located instructions on the drum
so each successive one was just past the read head
when it was needed;
the drum had to execute another complete revolution
to find the next instruction.
He coined an unforgettable term for this procedure.
Although “optimum” is an absolute term,
like “unique”, it became common verbal practice
to make it relative:
“not quite optimum” or “less optimum”
or “not very optimum”.
Mel called the maximum time-delay locations
the “most pessimum”.

After he finished the blackjack program
and got it to run
(“Even the initializer is optimized”,
he said proudly),
he got a Change Request from the sales department.
The program used an elegant (optimized)
random number generator
to shuffle the “cards” and deal from the “deck”,
and some of the salesmen felt it was too fair,
since sometimes the customers lost.
They wanted Mel to modify the program
so, at the setting of a sense switch on the console,
they could change the odds and let the customer win.

Mel balked.
He felt this was patently dishonest,
which it was,
and that it impinged on his personal integrity as a programmer,
which it did,
so he refused to do it.
The Head Salesman talked to Mel,
as did the Big Boss and, at the boss's urging,
a few Fellow Programmers.
Mel finally gave in and wrote the code,
but he got the test backwards,
and, when the sense switch was turned on,
the program would cheat, winning every time.
Mel was delighted with this,
claiming his subconscious was uncontrollably ethical,
and adamantly refused to fix it.

After Mel had left the company for greener pa$ture$,
the Big Boss asked me to look at the code
and see if I could find the test and reverse it.
Somewhat reluctantly, I agreed to look.
Tracking Mel's code was a real adventure.

I have often felt that programming is an art form,
whose real value can only be appreciated
by another versed in the same arcane art;
there are lovely gems and brilliant coups
hidden from human view and admiration, sometimes forever,
by the very nature of the process.
You can learn a lot about an individual
just by reading through his code,
even in hexadecimal.
Mel was, I think, an unsung genius.

Perhaps my greatest shock came
when I found an innocent loop that had no test in it.
No test. None.
Common sense said it had to be a closed loop,
where the program would circle, forever, endlessly.
Program control passed right through it, however,
and safely out the other side.
It took me two weeks to figure it out.

The RPC-4000 computer had a really modern facility
called an index register.
It allowed the programmer to write a program loop
that used an indexed instruction inside;
each time through,
the number in the index register
was added to the address of that instruction,
so it would refer
to the next datum in a series.
He had only to increment the index register
each time through.
Mel never used it.

Instead, he would pull the instruction into a machine register,
add one to its address,
and store it back.
He would then execute the modified instruction
right from the register.
The loop was written so this additional execution time
was taken into account —
just as this instruction finished,
the next one was right under the drum's read head,
ready to go.
But the loop had no test in it.

The vital clue came when I noticed
the index register bit,
the bit that lay between the address
and the operation code in the instruction word,
was turned on —
yet Mel never used the index register,
leaving it zero all the time.
When the light went on it nearly blinded me.

He had located the data he was working on
near the top of memory —
the largest locations the instructions could address —
so, after the last datum was handled,
incrementing the instruction address
would make it overflow.
The carry would add one to the
operation code, changing it to the next one in the instruction set:
a jump instruction.
Sure enough, the next program instruction was
in address location zero,
and the program went happily on its way.

I haven't kept in touch with Mel,
so I don't know if he ever gave in to the flood of
change that has washed over programming techniques
since those long-gone days.
I like to think he didn't.
In any event,
I was impressed enough that I quit looking for the
offending test,
telling the Big Boss I couldn't find it.
He didn't seem surprised.

When I left the company,
the blackjack program would still cheat
if you turned on the right sense switch,
and I think that's how it should be.
I didn't feel comfortable
hacking up the code of a Real Programmer.

Never knew you were a C++ programmer.

While the syntax is a little unpleasant, I'm... intrigued and slightly concerned at some of the implications there. But most of my C++ work is stuck back in ATL/MFC and C++6 code. So I don't get to go near those.

I understood.... half of that. Roughly...

Oh hello C++. I just started learning you! :yay: (Of course that means I understand almost none of that string of code... D:)

Pah, C++, FiM++ is clearly the language you want to be writing in.

The bad thing is that I am currently going through a C++ uni course, and really should understand what that means
:facehoof:

So if I paste this into CodeBlocks and run it, what will it do?

1380007
It's using stuff from C++2014 draft standard. If you don't have a c++14 compliant compiler, it will just error. I just use g++ to compile it.

As for what it does, it shows a horrid hack that combines c++'s object oriented inheritance rules and javascript's prototyped objects rules. It's really just a struct of functors under the hood, but it looks like `func` is a virtual class. But it isn't. And then I assign a new lambda to func and uses the old one as a base. It's awful and should never ever ever be done.

1379814

Kill it with fire!
:rainbowwild: LOL

But, I really cannot respect a language in which white space is assigned meaning.


1379826

The latest stable clang does support this, but the stable gcc branch (4.8) does not. And clang does not work on Windows.

I will reply to the rest when less tired.

1379842
Why aren't you using sourceforge or code.google.com? Then you could at least use SVN to do versioning.

1380053
So what you're saying is that you're Dr. Programming-Frankenstein? :derpytongue2:

Comment posted by kits deleted Sep 28th, 2013

1380055
Yeah. You need 4.9. This is the one found in MacPorts.
Hence the `g++-mp-4.9` call

1380102
>sourceforge
>google code
>svn
Welcome to 2005.
If I cared to version it, I'd use git and probably host on github. But this evil really shouldn't live on.

1380122
More like Dr Horrible.

edit. lol double post. WTG me

When it's objects, I go Java.
But when I want some good performance, I go with ANSI C. :raritywink:
Never liked C++ very much, though it's quite a fancy middle-ground between those 2. :pinkiehappy:

1380526
C++ is less about OO and more about generic programming. Throw in a huge pile of functional stuff, some objects to make things interesting, and a good dose of template based MPL (even if it's only CRTP to do static polymorphism) and you've hit most of what C++ does these days. C99 can do any C++ can, but often takes more lines of code.

Mmm, lambda functions.

1379789
1380853

One language to rule them all.
One language to bring them all and in the darkness to bind them.

Now onto programmable stacks!!!!! (laughs manically)

Because, why not?

(and then once they're done, make the entire thing interpreted)

1380999
Already in C++11 I'd like to point out.

As were closures.

And Monads.

The interesting part is where she overwrites D.func (that's right, functions are now diamonds!)

Didn't member functions used to be accessed through the vlookup table? I'm a bit rusty as I haven't touched C++ for a few years.

So, question, what happens when you modify the prototype of a superclass? (does it affect the subclass?)

1379789
1379782

btw, checkout JVM based languages.
You don't have to like Java to use the platform (I love the annotation based meta-programming and AOP - and you can mix and match languages :yay:)

http://en.wikipedia.org/wiki/List_of_JVM_languages

1381047

Didn't member functions used to be accessed through the vlookup table? I'm a bit rusty as I haven't touched C++ for a few years.

Yeah, they are. I haven't read up on C++11 yet though.

(Should probably start now. =/)

$~g++-mp-4.9 --std=c++1y test.cpp 2>&1

2>&1
why

1381047
These aren't member functions, they are member variables (of type std::function<>) whose values are being assigned to lambdas during the same statement they are declared. You used to not be able to do this and would have to assign them during the ctor, a dead giveaway they are functions. This way, the LOOK like function but aren't. I hijack inheritance to make d and f have a func member, just like an int, and then override the value of D's func member. You can only do this on an INSTANCE of a class, not the class itself, so changing an instance of s's func won't touch an instance of d or f's funcs.

1381047
I know about 'em. Scala is about the only one of use to most people. I like clojure because I am forced to do a lot o work in Matlab, which run in a JVM and thus has a java bridge. I like putting in snippets of clojure into my m. It discourages the users from meddling with the code (We are required to ship source: no p-coding.)

1381120>>1381047
They still are. These aren't member functions at all. They just look like it. Hence 'evil'.

1381149
So i can pipe it to less. It's a habit I picked up back when I was doing a lot of Boost.MPL work. STDOUT is worthless. I need STDERR in order to diagnose a template error. And those were usually in the 32kb range in terms of how much text was emitted.

1381162
Ohyeah Boost. I had to use that a few times, but generally stay away from it. Piping things through less makes sense though.

1379826

The thing I was referring to the weirdness of is the D programming language, which is the place I remember encountering the ability to set the default values of members like this.

Specifically in C++ when your write:

T lhs = std::move(rhs);

It just calls the correct operator overload. In the D language is instead effectively:

lhs = std::move(rhs);
rhs = T();

... unless you write a post move constructor in which it will then call that after setting the object to be the same as a default constructed object.

Kind of a waste, though I suppose it might get optimized by the compiler if it is deemed worth while by said compiler.

As to it looking like a member function, it is less evil if the std::function<> is private. I do not really see how simply being able to avoid writing a constructor in order to do this increases the potential for EVIL!, since that seems to be the only new thing.

And yes, std::function makes a copy unless the other object is movable. And you cannot capture by moving with lambdas unless the compiler has the new capture mode* from C++1y.

* I think it goes like:

D.func = [M = std::move(D)](){ return M.func()+ " goodbye"; };


1381047
1381120
1381159

A vtable is only for virtual member functions, and for that they are unlikely to ever leave. For non-virtual it is just like a calling a normal function, except with the 'this' pointer being passed.

In other words no change.

1381250
Right. What I was saying that after:

T lhs = std::move(rhs);

Accessing any part of `rhs` is Undefined Behavior. So it literally doesn't matter what it's values are. Touching it could format your HDD and still be following the specs.

If you make the member private, no one can call it. If you make it const, it can't be changed. If you want to not be evil at all, just make it a normal function. The purpose of this is to make something that looks easy to understand, but is really a veritable temple of doom to use.

And yes, [X=std::move(X)]{...} is how you capture a variable by rvalue reference in 1y. It has nothing to do at all with assignment to std::function.

A vtable has additional info. For one, it's where your RTTI lives if you're using that. Hence why typeid doesn't work on non-polymorphic objects. BUt yes. AFAIK, 1y makes no changes to the inheritance rules or class layout.

1381158

Right then. I guess it's not actually a prototyped object, and it's just syntactic sugar.

Also....

How the hell does the compiler know which function method to execute? I'm guessing some extra level of indirection, but does it do it for all methods now? Maybe it's optimized so that functions that are written to are treated actually as function pointers.

1382185
Where are you getting confused? The compiler knows which function to call the same way it might know what value to return if you have a public int field.

C+SWAG=YOLO

I think I did it wrong ._.

1382297

It requires storing *a reference* to the method in the object, as the method is modified at runtime.
Pretty sure most C++ implementations didn't used to do that.

http://stackoverflow.com/questions/15572411/where-are-member-functions-stored-for-an-object
(Feel free to post a new answer)

1382979
These aren't the same things at all. These are member variables, not member functions. The fact these variables are of type std::function doesn't change that (as hard as it is to believe.) So each one is stored "inside" the class as a std::function.

In this case, I'm not using any member functions except ctor. The member variables-acting-as-functions are essentially being stored by value.

Login or register to comment