Is there a good way of testing to make sure that an object was not copied?
You may try following check:
live demo
#include <boost/container/vector.hpp>
#include <iostream>
#include <ostream>
#include <vector>
#include <string>
using namespace boost;
using namespace std;
struct Test
{
bool copied;
Test()
: copied(false)
{
}
Test(const Test&)
: copied(true)
{
}
};
template<typename Container>
void check_move_constructor()
{
Container from(1);
Container to(boost::move(from));
cout << "\tmove constructor is" << (to[0].copied ? " not" : "") << " working" << endl;
}
template<typename Container>
void check_move_assignment()
{
Container from(1);
Container to;
to=boost::move(from);
cout << "\tmove assignment is" << (to[0].copied ? " not" : "") << " working" << endl;
}
template<typename Container>
void check_move(const string &name)
{
cout << name << " :" << endl;
check_move_constructor< Container >();
check_move_assignment< Container >();
cout << string(16,'_') << endl;
}
int main()
{
cout << boolalpha;
check_move< container::vector<Test> >("boost::container::vector");
check_move< vector<Test> >("std::vector");
return 0;
}
MSVC2008 outputs:
boost::container::vector :
move constructor is working
move assignment is working
________________
std::vector :
move constructor is not working
move assignment is not working
________________
Note, that in this code I used explicit move from lvalues, so Copy-elision can't work here.
P.S. another way to do this is to check generated assembler code. For instance /FA compiler option on MSVC or -S on GCC.
You can mark place of interest with special function call:
__declspec(noinline) void asm_marker(int line) { volatile int i=line; };
#define ASM_MARKER asm_marker(__LINE__)
And place that marker in code:
ASM_MARKER;
func_of_interest();
ASM_MARKER;
Asm code may look like:
mov ecx, 235 ; 000000ebH
call ?asm_marker@@YAXH@Z ; asm_marker
mov edi, r12d
lea rcx, QWORD PTR [rdi+rdi*4]
mov rax, QWORD PTR vec$[rsp]
lea r9, QWORD PTR [rax+rcx*8]
mov rbx, QWORD PTR vec$[rsp+32]
mov ecx, 237 ; 000000edH
call ?asm_marker@@YAXH@Z ; asm_marker
std::move
doesn't actually do anything other than return an r-valued reference to whatever is inside. I suppose a more specific way of asking what I want is,"I have an object that I think might have had its innards ripped out; how do I confirm that's actually what happened?"
– Airshipstd::move
. That's just a cast. I'm talking about a calling a function with something that binds to an rvalue reference. – Ebamemcmp
ing those. – Galloglassreinterpret_cast<char *>
and thenmemcmp
up tosizeof(Object)
? Sounds like the perfect solution. – Airshipreinterpret_cast
, thenmemcpy
into a buffer, and thenmemcmp
with the post-operation casted object. If your guts got ripped out, something should have changed. Of course, it's possible for a class to use the pimpl idiom and move from the implementation directly instead of moving the pointer to the implementation, but you're screwed anyways in that case. If that happens, and you class in question provides clear pre- and post-conditions for moving and ways to assess those, that might be another way (think.empty()
for containers). – Galloglass