Does Golang need the innovation of C++ move semantics too

643 views
Skip to first unread message

Li Jianhua

unread,
May 26, 2018, 1:48:23 AM5/26/18
to golan...@googlegroups.com

 

  1. Why does C++ introduce move semantics anyway? What problems is it going to solve?
  2. How come Golang, C (Without ++) don’t have that move semantics yet? Will they need the move soon?

 

 

 

Ian Lance Taylor

unread,
May 26, 2018, 2:57:07 AM5/26/18
to Li Jianhua, golan...@googlegroups.com
Move semantics in C++ permit classes that are expensive to copy to
provide a mechanism by which those classes can be moved rather than
copied. For example, in many implementations the C++ std::string
class can be expensive to copy, because it requires allocating new
memory for the copy and copying the string contents. When the
compiler can determine that the string object is being moved rather
than being copied, it can use move semantics to simply transfer the
pointer rather than copying the contents.

This does not apply to Go at all, because Go has no copy constructors.
In Go copying or moving a value is always straightforward.

Ian

Matthias B.

unread,
May 26, 2018, 10:52:47 AM5/26/18
to golan...@googlegroups.com
On Sat, 26 May 2018 03:15:45 +0000
Li Jianhua <lin...@outlook.com> wrote:

> 1. Why does C++ introduce move semantics anyway? What problems is
> it going to solve? 2. How come Golang, C (Without ++) don’t have
> that move semantics yet? Will they need the move soon?
>

C++ is a language used among other things for low level performance
critical applications, including microcontroller programming. Some of
these applications do not even have a traditional memory allocator. A
lot of the language is concerned with managing memory allocations and
related issues (constructors and destructors).

Giving the programmer control over move vs copy is useful/necessary in
C++ because of the existence of constructors and destructors. A simple
assignment a = b may cause multiple calls of complex functions that
may do things like obtain database locks etc. Move semantics allow the
programmer to avoid some of these calls in the common situation where a
copy is made and the original is immediately destroyed afterwards. In
this case, instead of constructing a whole new copy with all the
complexity involved, the programmer can arrange for a cheaper move to
be performed instead.

Go on the other hand is a garbage collected language that does not
allow the programmer to manually control allocation, object
construction or deconstruction. Before it would even make remote sense
to give the programmer control over the intricacies of object copying
vs moving there would have to be constructors and destructors added to
Go. For that to happen, probably more than 50% of Go's core team would
have to die.

MSB

--
If you do not fear death, how can you love life?

Li Jianhua

unread,
May 26, 2018, 11:22:58 AM5/26/18
to Ian Lance Taylor, golan...@googlegroups.com

From: Ian Lance Taylor <ia...@golang.org>
Sent: Saturday, May 26, 2018 2:56 PM
Subject: Re: [go-nuts] Does Golang need the innovation of C++ move semantics too
To: Li Jianhua <lin...@outlook.com>
Cc: <golan...@googlegroups.com>



On Fri, May 25, 2018 at 8:15 PM, Li Jianhua <lin...@outlook.com> wrote:

>
> Why does C++ introduce move semantics anyway? What problems is it going to
> solve?
> How come Golang, C (Without ++) don’t have that move semantics yet? Will
> they need the move soon?


This does not apply to Go at all, because Go has no copy constructors.
In Go copying or moving a value is always straightforward.

Ian

——

Will C++ do move more often and do copy less often with its specific move semantics. C++ will do move whenever possible, right?

Will Golang, C do copy more than C++ because Golang, C don’t have move semantics. So will C++ perform better than Golang, C?



Message has been deleted

T L

unread,
May 26, 2018, 11:58:58 AM5/26/18
to golang-nuts


Move is nothing special. It is just a pointer assignment plus a pointer reset.
You can achieve the move functionality in C and Go by custom code.
In fact, for many cases, pointer reset operations are not essential.
From the point of view, move is a little slower than non-move operations.

Hugh Fisher

unread,
May 26, 2018, 8:44:24 PM5/26/18
to golang-nuts


On Sunday, May 27, 2018 at 1:22:58 AM UTC+10, Li Jianhua wrote:


Will C++ do move more often and do copy less often with its specific move semantics. C++ will do move whenever possible, right?

Perhaps. It's difficult to tell, because C++ is a horribly complicated language. Will C++ use move semantics instead
of copy for smart pointers? Uh, maybe, if your smart pointer meets certain conditions ...

As others have pointed out, C++ needs this kind of complexity because with references etc the programmer cannot
tell just by looking at the code whether something is a move or copy. In C and Go, anything that is a pointer will use
"move" semantics, anything else is "copy". Simple rule, easy to follow, easy to see.


Will Golang, C do copy more than C++ because Golang, C don’t have move semantics. So will C++ perform better than Golang, C?

Golang will not copy more than C++ unless the programmer wants to. C++ is introducing move/copy semantics
because many C++ programmers write code that copies instead of moving without the programmer realising it.
The C++ solution is to add another layer of complexity to try and fix it. The C/Go solution is to keep it simple so
the problem does not happen in the first place.

Will C++ perform better than Golang, C? That will depend on the programmer, not the language. The move/copy
semantics are an example of micro-optimisation, trying to speed up tiny bits of the program. Usually it doesn't
matter. Many programs are written in Java, or Python, or JavaScript, where programmers almost never think
about move vs copy semantics at all, and those programs are fast enough for what they need to do.

cheers,
Hugh Fisher

Reply all
Reply to author
Forward
0 new messages