7

I'm working on testing whether or not my container wrappers are implementing URefs correctly. The only obvious way that I can think of is to try to figure out a way of detecting if an object has been moved.

Is there a good way of testing to make sure that an object was not copied? Or is there another way to test for what I want? I would prefer a solution that doesn't require modifying the classes I'm testing. Because there's a couple dozen of them.

Could you please provide some more info on what you have? Like what is your container, how you use it, can you modify it or not, etc. Maybe you can test this without modification of your container, but rather by using special container element type - which tracks copies and moves.

Several different containers and some standalone templated functions. Mostly, it's wrappers around STL library types like deque, list, map, set, etc.

OmnipotentEntity
  • 16,531
  • 6
  • 62
  • 96
  • "Moving from" isn't an actual activity like riding a bike. Rather, all that's happening is that a function has a mutable reference to your object. Having a reference isn't *doing* anything per se. It's only the grammatical basis for writing efficient code. – Kerrek SB Nov 11 '12 at 00:10
  • I know that `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?"` – OmnipotentEntity Nov 11 '12 at 00:14
  • I'm not even talking about `std::move`. That's just a cast. I'm talking about a calling a function with something that binds to an rvalue reference. – Kerrek SB Nov 11 '12 at 00:16
  • 1
    I think the only way this *might* be possible is by looking directly at the objects binary representation before and after the operation in question, and then `memcmp`ing those. – Xeo Nov 11 '12 at 00:29
  • @Xeo, so `reinterpret_cast` and then `memcmp` up to `sizeof(Object)`? Sounds like the perfect solution. – OmnipotentEntity Nov 11 '12 at 00:32
  • 1
    Mostly right. `reinterpret_cast`, then `memcpy` into a buffer, and then `memcmp` 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). – Xeo Nov 11 '12 at 00:36
  • 1
    For future reference, inspection of memory doesn't work in all cases. Use the accepted answer. – OmnipotentEntity Nov 11 '12 at 04:27

2 Answers2

2

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   
Evgeny Panasyuk
  • 9,076
  • 1
  • 33
  • 54
  • Ok, I'll amend my question, do you have a good answer to how I can test for this? – OmnipotentEntity Nov 10 '12 at 23:46
  • 1
    While useful and very informative, I was hoping to add this to my test suite, rather than having it be something I must do by hand periodically. Thank you for the info though. It is definitely useful. – OmnipotentEntity Nov 11 '12 at 00:01
  • Could you please provide some more info on what you have? Like what is your container, how you use it, can you modify it or not, etc. Maybe you can test this without modification of your container, but rather by using special container element type - which tracks copies and moves. – Evgeny Panasyuk Nov 11 '12 at 00:04
  • "Ok, I'll amend my question, do you have a good answer to how I can test for this?" - now I understand your question better. In fact you can check your move operations, by using context where Copy-elision/RVO/NRVO can't be applied. So you can change your question to something like "How to check that move constructor and move assignment work?" – Evgeny Panasyuk Nov 11 '12 at 00:33
  • I actually want to check both cases, but I think I have enough tools between you and Xao to make that happen right now. Thank you very much for your effort, it's been extremely helpful. – OmnipotentEntity Nov 11 '12 at 00:35
1

Add a boolean field that's set to false when the object is constructed. In the move constructor and move assignment operator, assign true to that field in the object being moved from.

Pete Becker
  • 74,985
  • 8
  • 76
  • 165
  • 1
    Stepping back, it seems that for the scope of the work I'm trying to do, this won't be practical. Is there a method possible without modifying the classes I'm testing? – OmnipotentEntity Nov 10 '12 at 23:58