Allegro.cc - Online Community

Allegro.cc Forums » Off-Topic Ordeals » [rant] C++0x is ...so simple.

This thread is locked; no one can reply to it. rss feed Print
 1   2   3   4 
[rant] C++0x is ...so simple.
axilmar
Member #1,204
April 2001

C++0x is ...so simple. I mean, what are 30* pages of explanation between friends? almost nothing!!!

[/irony]

*at 1280x1024

kazzmir
Member #1,786
December 2001
avatar

Thats a pretty interesting article, thanks for pointing it out!

Matthew Leverton
Supreme Loser
January 1999
avatar

It was only three pages. :-/

{"name":"599214","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/a\/4\/a4149249b2f1d2e530ce0b4c56db8166.png","w":1200,"h":1920,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/a\/4\/a4149249b2f1d2e530ce0b4c56db8166"}599214

ixilom
Member #7,167
April 2006
avatar

Only one page here, just needed to scroll a bit.

___________________________________________
Democracy in Sweden? Not since 2008-Jun-18.
<someone> The lesbians next door bought me a rolex for my birthday.
<someone> I think they misunderstood when I said I wanna watch...

Goalie Ca
Member #2,579
July 2002
avatar

Actually, that was just covering rvalues and associated topics. I'm still trying to wrap my head around it and how to properly write code that will use it. This will be probably huge in my scientific computing for performance.

-------------
Bah weep granah weep nini bong!

Martin Kalbfuß
Member #9,131
October 2007
avatar

Is the font only small in my webbrowser? My eyes hurt. When I see such pages.

http://remote-lisp.spdns.de -- my server side lisp interpreter
http://www.nongnu.org/gm2/ -- Modula-2 alias Pascal++

Matthew Leverton
Supreme Loser
January 1999
avatar

Why have you started using excessive periods (stops)?

X-G
Member #856
December 2000
avatar

Goalie Ca said:

I'm still trying to wrap my head around it and how to properly write code that will use it

Aside from lambdas, rvalue references are probably the feature I am most excited about. For something so simple, it provides so much elegance for the very crucial features of move semantics and perfect forwarding.

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

anonymous
Member #8025
November 2006

The downside seems to be that you might need lots of overloads for things. For example, std::string::operator+ has a whole lot of new overloads for rvalue references.

What would be interesting is to figure out how to get your objects prematurely destroyed by moving as people fear (find out how the move semantics of rvalue references is similar and dissimilar to std::auto_ptr). Below I have two ways (but not that impressive since you explicitly ask for it):

#SelectExpand
1#include <iostream> 2 3class X 4{ 5 int* p; 6public: 7 X(): p(0) { std::cout << "X()\n"; } 8 X(int n): p(new int(n)) { std::cout << "X(int)\n"; } 9 X(const X&) = delete; 10 X(X&& x): p(x.p) { x.p = 0; std::cout << "X(X&& x)\n"; } 11 X& operator=(const X&) = delete; 12 X& operator= (X&& other) 13 { 14 if (this != &other) { 15 delete p; 16 p = other.p; 17 other.p = 0; 18 } 19 std::cout << "operator=(X&&)\n"; 20 return *this; 21 } 22 ~X() {delete p; std::cout << "~X()\n"; } 23 void print() const 24 { 25 if (p) { 26 std::cout << *p << '\n'; 27 } 28 else { 29 std::cout << "NULL\n"; 30 } 31 } 32}; 33 34void foo(X&& x) 35{ 36 std::cout << "foo\n"; 37 x.print(); 38 X y(std::move(x)); 39 y.print(); 40} 41 42int main() 43{ 44 X a(10), b(20); 45 a.print(); 46 b.print(); 47 //a = b; //doesn't compile X::operator=(const X&) deleted 48 a = std::move(b); 49 a.print(); 50 b.print(); 51 foo(a); //works, but doesn't move a 52 a.print(); 53 foo(std::move(a)); //compiles, but still doesn't move a 54 a.print(); 55 foo(X(30)); 56}

Matthew Leverton
Supreme Loser
January 1999
avatar

Why don't they name it C++&&<<=*?

X-G
Member #856
December 2000
avatar

anonymous said:

The downside seems to be that you might need lots of overloads for things.

Not need; Rvalue&& overloads are strictly optional. Going without them only means you fail to take advantage of move-semantics -- something you couldn't do in plain C++ anyway, and your code will work just fine without any additional overloads. It'll just do more copies than is necessary.

Quote:

but not that impressive since you explicitly ask for it

I don't think it counts if you explicitly shoot yourself in the foot by attempting to move an object you intend to keep using...

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

anonymous
Member #8025
November 2006

X-G said:

I don't think it counts if you explicitly shoot yourself in the foot by attempting to move an object you intend to keep using...

There was a similar thread where someone argued that even if you need to be explicit about it, it just makes it too easy for junior programmers to destroy stuff senior programmers want to keep using. I suppose the argument goes that those juniors won't bother to read / understand the linked article (or similar) but know that rvalue references and std::move are good for performance...

Goalie Ca
Member #2,579
July 2002
avatar

C++ is a language I absolutely hate because it takes a demi-god to really understand it. You can become quite dangerous quite quickly but there is sooo much behind the scenes that you need to understand and incorporate in all levels of design. In this, C++ fails proper abstraction.

-------------
Bah weep granah weep nini bong!

X-G
Member #856
December 2000
avatar

Personally, I don't trust language abstractions as a means of protecting someone from his own ineptitude. If anything, I think it makes it harder to spot them and weed them out or at least give them proper guidance; and at any rate, an inept person will screw up regardless of how much the language tries to shield him from it. That is not the purpose of language abstractions.

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

axilmar
Member #1,204
April 2001

anonymous said:

There was a similar thread where someone argued that even if you need to be explicit about it, it just makes it too easy for junior programmers to destroy stuff senior programmers want to keep using. I suppose the argument goes that those juniors won't bother to read / understand the linked article (or similar) but know that rvalue references and std::move are good for performance...

The Academia leans heavily towards non-destructive updates - Stroustrup and Co. think the opposite. I expect some big disasters from move semantics, since data will mysteriously vanish...the only reason move semantics exist because the C++ people hate garbage collection with a passion (and therefore manual memory management forces them not to return pointers to objects but wrappers that copy objects - and now that they move objects). If they really cared about performance, they would return pointers - or making everything implicitly shared, ala QString.

anonymous
Member #8025
November 2006

axilmar said:

I expect some big disasters from move semantics, since data will mysteriously vanish

But it vanishes mysteriously only from temporaries as they go out of scope?

Ok, perhaps they should have followed their example of reinterpret_cast<T> (it's so ugly and long to keep people from using it), and named std::move std::move_it_so_I_might_never_see_my_data_again instead.

Quote:

If they really cared about performance, they would return pointers

Which will just trade one kind of potential errors for another? For one thing, you can't return pointers to locals (optimizing returning by value seems to be one of the points of move semantics - doesn't it transparently move the return value out of the function, as long as a move constructor/assignment exists?). So there will be lots more of dynamic allocation?

Tobias Dammers
Member #2,604
August 2002
avatar

X-G said:

Personally, I don't trust language abstractions as a means of protecting someone from his own ineptitude. If anything, I think it makes it harder to spot them and weed them out or at least give them proper guidance; and at any rate, an inept person will screw up regardless of how much the language tries to shield him from it. That is not the purpose of language abstractions.

A better reason for language abstractions is to reduce boilerplate code and signal the intention of a piece of code rather than the low-level implementation. This is always a balancing act of course.

---
Me make music: Triofobie
---
"We need Tobias and his awesome trombone, too." - Johan Halmén

bamccaig
Member #7,536
July 2006
avatar

I think rvalue references are a great idea. :) I'm only half way through the part 2 post now and I still have to go back and read part 1... I'm excited to see what other features to expect and what they'll look like (I may have already read about them, but I don't recall).

amarillion
Member #940
January 2001
avatar

I'm definitely going to use lambda's, that's a really useful addition and I can think of a few places in my games where they might apply.
auto is a no-brainer. Soon, nobody will remember how to live without.
I'll probably only use rvalue references through the standard library.

C++ is continuing to be the smorgasbord of programming languages. Just pick what you like :)

anonymous
Member #8025
November 2006

auto is a no-brainer. Soon, nobody will remember how to live without.

:)

I made a wrapper for <algorithms> and found that some functions return an iterator or a const_iterator depending on the constness of the range. To avoid writing the same functions twice I came up with this:

#SelectExpand
1 namespace detail 2 { 3 template <class Range> 4 struct Iterator 5 { 6 typedef typename Range::iterator type; 7 }; 8 9 template <class Range> 10 struct Iterator<const Range> 11 { 12 typedef typename Range::const_iterator type; 13 }; 14 } 15 16 //usage: 17 template <class Range, class Type> 18 typename detail::Iterator<Range>::type find(Range& range, const Type& value);

Now, with auto there's no trickery needed:

template <class Range, class Type>
auto find(Range& range, const Type& value) -> decltype(range.begin());

//or ... -> decltype(std::find(range.begin(), range.end(), value))

rvalue references seem to require lots of overloads to use it actively: as I understand you can only take advantage of move semantics (std::move) safely when a) the functions leaves everything in a good state (e.g swap), b) std::move is used on a rvalue reference parameter if there is an overload that takes that parameter by const lvalue reference (so that the moving version is only used with the temporaries).

X-G
Member #856
December 2000
avatar

Of course it's possible to screw up, and it always will be, but I see nothing more difficult about using std::move properly than, say, designing copy semantics properly or even plain using pointers properly. Sure you can trip up, and you probably will, but no one is asking you to be perfect. The opportunities opened by move semantics, not just for performance but for finally being able to have proper noncopyable-but-movable objects (a more common usecase than one might think), far outweigh any initial difficulty encountered.

That wrapper looks similar to some stuff we have at work. Best stuff since sliced bread.

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

axilmar
Member #1,204
April 2001

anonymous said:

But it vanishes mysteriously only from temporaries as they go out of scope?

Temporaries may be passed to functions that keep pointers to them.

Quote:

Which will just trade one kind of potential errors for another? For one thing, you can't return pointers to locals (optimizing returning by value seems to be one of the points of move semantics - doesn't it transparently move the return value out of the function, as long as a move constructor/assignment exists?). So there will be lots more of dynamic allocation?

Which error are you talking about? I don't mean returning a pointer to a local, I mean allocating something on the heap and returning a pointer to it. Move semantics would not be required if functions returned pointers to heap data - but then C++ would need garbage collection, which is something the C++ committee does not want.

I wonder why they did not use implicit sharing though. Swap for vectors with implicit sharing is a simple pointer swap, for example. Any data structure that has value semantics but it has heap data internally is best treated with implicit sharing.

Goalie Ca
Member #2,579
July 2002
avatar

Objects being constructed and destructed inside STL have "mysteriously" crashed many people's code. C++ fails to actually encapsulate the inner workings in far too many cases. The language is a mess but until momentum shifts to something more sane then I guess I am stuck.

I absolutely love the idea of bytecode and VMs. I can write the numerical part of my code in a functional language, the gui in an object oriented one, and do scripting via some interactive one all without writing language bindings.

-------------
Bah weep granah weep nini bong!

anonymous
Member #8025
November 2006

axilmar said:

Temporaries may be passed to functions that keep pointers to them.

Example, please. How do I take a pointer to a temporary and manage to pass it to a function (as a rvalue reference) at the same time? In addition, would you really take the address of a temporary?

Quote:

I wonder why they did not use implicit sharing though. Swap for vectors with implicit sharing is a simple pointer swap, for example. Any data structure that has value semantics but it has heap data internally is best treated with implicit sharing.

I don't entirely understand. Do you suggest that implementing copy-on-write (?) for classes is simpler? Could you show an example of how you'd implement a swap for a vector (and do rvalue references have only to do with swapping)?

Quote:

Move semantics would not be required if functions returned pointers to heap data - but then C++ would need garbage collection, which is something the C++ committee does not want.

I have a feeling that a non-optional GC would just turn C++ into a completely different language (why not program in a GC language in the first place?) and optionally not using it would be out of the question if the standard way of doing things would be what you describe.

ImLeftFooted
Member #3,935
October 2003
avatar

X-G said:

Personally, I don't trust language abstractions as a means of protecting someone from his own ineptitude. If anything, I think it makes it harder to spot them and weed them out or at least give them proper guidance; and at any rate, an inept person will screw up regardless of how much the language tries to shield him from it. That is not the purpose of language abstractions.

Hear hear!

There is too much language-based oppression out there "for your own good."

 1   2   3   4 


Go to: