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
wat
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.
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!
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
hell yes
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! (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.
1379861 TL;DR
The bad thing is that I am currently going through a C++ uni course, and really should understand what that means
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!
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?
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.
Never liked C++ very much, though it's quite a fancy middle-ground between those 2.
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 )
http://en.wikipedia.org/wiki/List_of_JVM_languages
1381047
Yeah, they are. I haven't read up on C++11 yet though.
(Should probably start now. =/)
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:
It just calls the correct operator overload. In the D language is instead effectively:
... 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:
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:
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.