Skip to content

ChaiScript assumes a non-const object reference is an R-value (&&) #339

@dinghram

Description

@dinghram
  • Compiler Used: Visual Studio 2015 R3
  • Operating System: Windows 7
  • Architecture (ARM/x86/32bit/64bit/etc): 64bit

Expected Behavior

I added copy ctor, move ctor and copy/move = operators for my object to my ChaiScript engine. I had a function that returned a non-const object reference. When I call this function in my script and catch the returned value in an object, I'd expect the copy operator = to be invoked.

Actual Behavior

The returned object got passed to the move = operator rather than the copy = operator, destroying my object.

Minimal Example to Reproduce Behavior

C++:
class O
{
public:
O() {}
O( const O& ) {}
O( O&& ) {}
O& operator=( const O& ) { return *this; }
O& operator=( O&& ) { return *this; }
};

module.add( chaiscript::user_type<O>(), "O" );
module.add( chaiscript::constructor<O()>(), "O" );
module.add( chaiscript::constructor<O(const O&)>(), "O" );
module.add( chaiscript::constructor<O(O&&)>(), "O" );
module.add( chaiscript::fun( static_cast<O& ( O::* )( const O& )>( &O::operator= ) ), "=" );
module.add( chaiscript::fun( static_cast<O& ( O::* )( O&& )>( &O::operator= ) ), "=");
module.add( chaiscript::fun( []()->O& { static O o; return o; } ), "getO" );

ChaiScript:
var o = getO(); // invokes move ctor on object returned from "getO"

var o2 = O();
o2 = getO(); // invokes move operator = on object returned from "getO"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions