std::move() gives error "invalid use of void expression"

Hi ppl :),

I have a vector of unique_ptr and I am trying to move them to another array but getting the error "invalid use of void expression".
I searched over the internet but could find this error linked only with "void*".

Here is the code snippet where the error pops up.

1
2
3
4
5
6
7
8
9
10
11
12
std::unique_ptr<Slot> freeSlots;
std::unique_ptr<Slot> filledSlots;

for (register unsigned int i = 0; i < 10; ++i)
    freeSlots.push_back(std::make_unique<Slot>(i));  //1) Can we use
   //std::move here ? will it be more efficient assuming Slot is a heavy to build object

long int ParkingLot::DriveIn()
{
    filledSlots.push_back(std::move(freeSlots.pop_back())); //2) gives error
    return 10;
}


I have inlined the questions as 1) and 2).

Any help would be really great. Also could you kindly suggest me some good reads for move semantics... I am always confused in this part.. :(

Thanks
Last edited on
kapil2905 wrote:
freeSlots.push_back(std::make_unique<Slot>(i)); // 1) Can we use std::move here ? will it be more efficient assuming Slot is a heavy to build object

push_back will move out of its rvalue argument. std::move would do nothing in this case.

kapil2905 wrote:
filledSlots.push_back(std::move(freeSlots.pop_back())); //2) gives error

pop_back() returns void (see http://en.cppreference.com/w/cpp/container/vector/pop_back or http://www.cplusplus.com/reference/vector/vector/pop_back/ assuming freeSlots and filledSlots are vectors based on "I have a vector" - they aren't in your code) . You can't use an expression that returns void as an argument to std::move or to any other function. That's what the compiler error is pointing out.
You can std::move out of freeSlots.back(), and then pop_back() to get rid of its moved-from shell.

kapil2905 wrote:
register unsigned int i = 0

the keyword register never had a meaning in C++ and was removed in C++17
Last edited on
1) Using std::move() explicitly makes no difference here. Only a single Slot is constructed.
2) All of the pop_back() functions in standard containers return nothing. You probably meant
1
2
filledSlots.push_back(std::move(freeSlots.back()));
freeSlots.pop_back();
Last edited on
Thanks @Cubbi and @helios for pointing out the mistake and clarifying my doubt for ques 1. :)
Registered users can post here. Sign in or register to post.