EzDevInfo.com

move.js

CSS3 backed JavaScript animation framework Move.js - CSS3 animation framework for JavaScript

What is move_iterator for

If I understand it correct, a=std::move(b) binds reference a to the address of b. And after this operation the content that b points to is not guaranteed.

The implementation of move_iterator here has this line

auto operator[](difference_type n) const -> decltype(std::move(current[n]))
  { return std::move(current[n]); }

However, I don't think it makes sense to std::move an element in an array. What happens if a=std::move(b[n])?

The following example confuses me also:

std::string concat = std::accumulate(
                             std::move_iterator<iter_t>(source.begin()),
                             std::move_iterator<iter_t>(source.end()),
                             std::string("1234"));

Since the concat will itself allocate a continuous chunk of memory to store the result, which will not have any overlap with source. The data in source will be copied to concat but not moved.


Source: (StackOverflow)

How to store objects without copy or move constructor in std::vector?

To improve efficiency of std::vector<T>, it's underlying array needs to be pre-allocated and sometimes re-allocated. That, however, requires the creation and later moving of objects of type T with a copy ctor or move ctor.

The problem that I am having is that T cannot be copied or moved because it contains objects that cannot be copied or moved (such as atomic and mutex). (And, yes, I am implementing a simple thread pool.)

I would like to avoid using pointers because:

  1. I do not need a level of indirection and so I do not want one.
  2. (Pointers are less efficient and increase complexity. Using pointers increases memory fragmentation and decreases data locality which can (but not necessarily must) cause a noticeable performance impact. Not so important, but still worth consideration.)

Is there a way to avoid a level of indirection here?

UPDATE: I fixed some incorrect assumptions and re-phrased the question, based on feedback in comments and answers.


Source: (StackOverflow)

Advertisements

How to move table from one tablespace to another in oracle 11g

I run oracle 11g and need to move table (tbl1) from one tablespace (tblspc1) to another (tblspc2). What is the easiest way to do that?


Source: (StackOverflow)

"move" two vectors together

If I have two vectors and want to combine them to one, I can do it the following way:

std::vector<T> a(100); // just some random size here
std::vector<T> b(100);

a.insert(std::end(a), std::begin(b), std::end(b));

That involves copying though, which I want to avoid. Is there any way to use move-semantics to get them together?
I highly doubt it, as a vector is supposed to be contiguous. However is there any way to do it with a deque?


Source: (StackOverflow)

How to correctly "perfect forward" getter functions?

I'm creating a JSON library for C++14 and I'm trying to make use of move semantics whenever possible.

My Value class has several setters and getters that always try to move when possible:

template<class T> void setObj(T&& x)  { type = Obj; hObj.init(forward<T>(x)); } 
template<class T> void setArr(T&& x)  { type = Arr; hArr.init(forward<T>(x)); }
template<class T> void setStr(T&& x)  { type = Str; hStr.init(forward<T>(x)); }

auto& getObj() & noexcept             { assert(is<Obj>()); return hObj; }
auto& getArr() & noexcept             { assert(is<Arr>()); return hArr; }
auto& getStr() & noexcept             { assert(is<Str>()); return hStr; }
const auto& getObj() const& noexcept  { assert(is<Obj>()); return hObj; }
const auto& getArr() const& noexcept  { assert(is<Arr>()); return hArr; }
const auto& getStr() const& noexcept  { assert(is<Str>()); return hStr; }
auto getObj() && noexcept             { assert(is<Obj>()); return move(hObj); }
auto getArr() && noexcept             { assert(is<Arr>()); return move(hArr); }
auto getStr() && noexcept             { assert(is<Str>()); return move(hStr); }

As you can see from the code, perfect forwarding setter functions is pretty easy using templates and universal references.

How can I do the same for getter functions? I'm pretty sure I have to use a template return type but I'm not sure how to replicate ref-qualifiers and const-correctness.


Source: (StackOverflow)

When both move and copy constructor is present, which one will be called?

Below is class A which is full of different type of constructor. If i comment the move constructor, then the copy constructor is called twice : once for passing an object to function fun by value and other by returning from the same function.

Code Snippet

class A {

int x;

public :
A() {
    cout<<"Default Constructor\n";
}

A(A&& a) : x(a.x){
    cout<<"Move Constructor\n";
    a.x=0;
}

A(const A& a){
    x=a.x;
    cout<<"Copy Constructor\n";
}

A fun(A a){
    return a;
}

};

int main() {

A a;
A b;
A c;
c=a.fun(b);

}

OUTPUT :

Default Constructor

Default Constructor

Default Constructor

Copy Constructor

Move Constructor

However, if the move constructor is present, it is called rather than copy constructor. Can anyone eloborate this with a good example, so that i will be clear on this concept.

I would appreciate your help.Thanks.


Source: (StackOverflow)

Raphael JS : how to move/animate a path object?

Somehow this doesn't work...

var paper = Raphael("test", 500, 500);

var testpath = paper.path('M100 100L190 190');

var a = paper.rect(0,0,10,10);
a.attr('fill', 'silver');

a.mousedown( function() {
  testpath.animate({x: 400}, 1000);
});

I can move rects this way but not paths, why is that, and how do I move a path object then?!


Source: (StackOverflow)

Is a moved-from shared_ptr guaranteed to be emptied?

Consider the following code:

struct Bar
{
    std::shared_ptr<int> MemberFunction()
    {
        return std::move(m_memberVariable);
    }

    std::shared_ptr<int> m_memberVariable;
};

Is it guaranteed that the std::move from a shared_ptr<T> will actually remove the reference in the member variable? Or should I copy, clear and return copy to guarantee this*

Clearly in the case of unique_ptr<T> it does the right thing (it cannot possibly not do) but does the standard guarantee that a std::moved from shared_ptr releases its reference? [when it is a member variable, static or global, locals don't matter as they go out of scope]

*possibly "swap and return" is better than "copy, clear and return".


Source: (StackOverflow)

How do I return a non-movable (but copyable) object?

Edit: End goal: I want to make a container class that never uses move, even when it's available. NonMove is a class of test objects for that container.

I tried different variations, but GCC insists on wanting to use move.

class NonMove {
 public:
  NonMove() {}

  // Copy.
  NonMove(const NonMove&) {}
  NonMove& operator=(const NonMove&) {}

  // Move
  NonMove(NonMove&&) = delete;
  NonMove& operator=(NonMove&&) = delete;
};

NonMove foo() {
  return NonMove();
}

Error with GCC 4.9.1 with -std=gnu++11

move.cc: In function ‘NonMove foo()’:
move.cc:15:18: error: use of deleted function ‘NonMove::NonMove(NonMove&&)’
   return NonMove();
                  ^
move.cc:10:3: note: declared here
   NonMove(NonMove&&) = delete;
   ^

Source: (StackOverflow)

is there any difference between static cast to rvalue reference and std::move

The description for static cast says

If new_type is an rvalue reference type, static_cast converts the value of expression to xvalue. This type of static_cast is used to implement move semantics in std::move.(since C++11)

Does this confirm that the following are equivalent ?

(A)

X x1;
X x2 = static_cast<X&&>(x1); 

(B)

X x1;
X x2 = std::move(x1);

Source: (StackOverflow)

How can I avoid wasteful copying of keys in a B-tree based STL-like map?

I'm replacing a use of std::map in a hot path with cpp-btree's btree_map. But with optimization enabled, GCC and Clang complain about a strict aliasing violation. The problem boils down to this:

template <typename Key, typename Value>
class btree_map {
public:
    // In order to match the standard library's container interfaces
    using value_type = std::pair<const Key, Value>;

private:
    using mutable_value_type = std::pair<Key, Value>;

    struct node_type {
        mutable_value_type values[N];
        // ...
    };

public:
    class iterator {
        // ...

        value_type& operator*() {
            // Here we cast from const std::pair<Key, Value>&
            // to const std::pair<const Key, Value>&
            return reinterpret_cast<value_type&>(node->values[i]);
        }
    };

    std::pair<iterator, bool> insert(const value_type& value) {
        // ...
        // At this point, we have to insert into the middle of a node.
        // Here we rely on nodes containing mutable_value_type, because
        // value_type isn't assignable due to the const Key member
        std::move_backward(node->values + i, node->values + j,
                           node->values + j + 1);
        node->values[i] = value;
        // ...
    }
};

This got me thinking, is there a way to do this as efficiently that doesn't rely on undefined behaviour? The keys I'm using are efficiently moveable but fairly slow to copy, so I'd love to avoid copying many keys on every insertion. I've considered

  • Using value_type values[N], then const_cast<Key&>(values[i].first) = std::move(key) to move the key around, but I'm pretty sure that's still undefined
  • Returning std::pair<const Key&, Value&> instead of std::pair<const Key, Value>& when appropriate, but I'm not sure this would still satisfy the container requirements (I hear ...::reference is supposed to really be a reference type)
  • Not caring. The code works as-is, but I'm curious if it can be done in a standard-compliant way. There's also the chance that future compilers do different things with the UB, and I don't know of a way to apply -fno-strict-aliasing to only a single class.

Any other ideas?


Source: (StackOverflow)

Should I always move on `sink` constructor or setter arguments?

struct TestConstRef {
    std::string str;
    Test(const std::string& mStr) : str{mStr} { }
};

struct TestMove {
    std::string str;
    Test(std::string mStr) : str{std::move(mStr)} { }
};

After watching GoingNative 2013, I understood that sink arguments should always be passed by value and moved with std::move. Is TestMove::ctor the correct way of applying this idiom? Is there any case where TestConstRef::ctor is better/more efficient?


What about trivial setters? Should I use the following idiom or pass a const std::string&?

struct TestSetter {
    std::string str;
    void setStr(std::string mStr) { str = std::move(str); }
};

Source: (StackOverflow)

How to move a marker in Google Maps API

I'm using the Google Maps API V3 and I'm trying to make a marker move across the screen. Here's what I have:

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<style type="text/css">
  html { height: 100% }
  body { height: 100%; margin: 0; padding: 0 }
  #map_canvas { height: 100% }
</style>
<script type="text/javascript"
    src="http://maps.googleapis.com/maps/api/js?sensor=false">
</script>
<script type="text/javascript">
function initialize() {
  var myLatLng = new google.maps.LatLng(50,50);
  var myOptions = {
    zoom: 4,
    center: myLatLng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  }
  var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

  image = 'bus.gif';
  marker = new google.maps.Marker({position: myLatLng, map: map, icon: image});

  marker.setMap(map);
}

function moveBus()
{
  // Move Bus
}

</script>
</head>

<body onload="initialize()">
<script type="text/javascript">
moveBus();
</script>
<div id="map_canvas" style="height: 500px; width: 500px;"></div>

</body>
</html>

Now what I've tried is replacing // Move Bus with

marker.setPosition(new google.maps.LatLng(0,0));

But that didn't do anything. That's the command I saw on the API reference. I'm also relatively new to Javascript, so it might be a JS error on my part.


Source: (StackOverflow)

Conveniently move a class to a different package in eclipse without borking svn

When moving a file from old.package to new.package I want two things to happen:

  1. Update all references to that class (in all files of the project) so that the new package is used
  2. svn move old/package/Foo.java new/package/Foo.java

I use subversive within Eclipse Ganymede. When I just drag the file from one package to the other, all references get updated and the file is moved around on the filesystem. But SVN is unaware of this and therefore the svn move old/package/foo.java new/package/Foo.java command does not work (obviously) when old/package/Foo.java does not exist (because eclipse has moved it already).

Is there a way to have eclipse do it's refactoring but using svn to move the file on the filesystem? I hope I'm just missing something obvious :-)


Source: (StackOverflow)

Usage of std::forward vs std::move

I always read that std::forward is only for use with template parameters. However, I was asking myself why. See the following example:

void ImageView::setImage(const Image& image){
    _image = image;
}

void ImageView::setImage(Image&& image){
    _image = std::move(image);
}

Those are two functions which basically do the same; one takes an l-value reference, the other an r-value reference. Now, I thought since std::forward is supposed to return an l-value reference if the argument is an l-value reference and an r-value reference if the argument is one, this code could be simplified to something like this:

void ImageView::setImage(Image&& image){
    _image = std::forward(image);
}

Which is kind of similar to the example cplusplus.com mentions for std::forward (just without any template parameters). I'd just like to know, if this is correct or not, and if not why.

I was also asking myself what exactly would be the difference to

void ImageView::setImage(Image& image){
    _image = std::forward(image);
}

Source: (StackOverflow)