overloaded comparison operator not getting called

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

overloaded comparison operator not getting called

piers august
Hi,

I can't see why the comparison operator for the MyInt type below isn't being called - can anyone help? I'm sure I've overlooked something obvious.
In lua, a comparison on a MyInt and an int type always returns false and the non member comparison operator is never called.
The above comparison works fine in C++.

Code snippets and details below.

class MyInt
{
    public:
    MyInt(int value):value_(value){}
    int value()const{return value_;}
   
    private:
    const int value_;
};

bool operator==(const MyInt& myInt,int i)
{
    std::cout<<"here"<<std::endl;
    return myInt.value()==i;
}

luabind::module(L)
    [
        luabind::class_<MyInt>("MyInt")
        .def(luabind::constructor<int>())
        .def(luabind::self==int())
        .def(luabind::const_self==int())
    ];


Lua
>
myInt=MyInt(2013)
year=2013
b=(myInt==year)
print(b)


false


C++
MyInt myInt(2013);
 std::cout<<"MyInt test="<<(myInt==2013)<<std::endl;
 std::cout<<"MyInt test="<<(myInt==2012)<<std::endl;

here
MyInt test=1
here
MyInt test=0

BOOST_LIB_VERSION=1_48
LUA_VERSION_NUM=501
LUABIND_VERSION=900
G++ VERSION=4.4.6 20110731 (Red Hat 4.4.6-3)

Many thanks
Piers


------------------------------------------------------------------------------
Master Visual Studio, SharePoint, SQL, ASP.NET, C# 2012, HTML5, CSS,
MVC, Windows 8 Apps, JavaScript and much more. Keep your skills current
with LearnDevNow - 3,200 step-by-step video tutorials by Microsoft
MVPs and experts. ON SALE this month only -- learn more at:
http://p.sf.net/sfu/learnmore_122712
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: overloaded comparison operator not getting called

liam mail
On 10 January 2013 14:00, piers august <[hidden email]> wrote:

> Hi,
>
> I can't see why the comparison operator for the MyInt type below isn't being
> called - can anyone help? I'm sure I've overlooked something obvious.
> In lua, a comparison on a MyInt and an int type always returns false and the
> non member comparison operator is never called.
> The above comparison works fine in C++.
>
> Code snippets and details below.
>
> class MyInt
> {
>     public:
>     MyInt(int value):value_(value){}
>     int value()const{return value_;}
>
>     private:
>     const int value_;
> };
>
> bool operator==(const MyInt& myInt,int i)
> {
>     std::cout<<"here"<<std::endl;
>     return myInt.value()==i;
> }
>
> luabind::module(L)
>     [
>         luabind::class_<MyInt>("MyInt")
>         .def(luabind::constructor<int>())
>         .def(luabind::self==int())
>         .def(luabind::const_self==int())
>     ];
>
>
> Lua
>>
> myInt=MyInt(2013)
> year=2013
> b=(myInt==year)
> print(b)
>
>
> false
>
>
> C++
> MyInt myInt(2013);
>  std::cout<<"MyInt test="<<(myInt==2013)<<std::endl;
>  std::cout<<"MyInt test="<<(myInt==2012)<<std::endl;
>
> here
> MyInt test=1
> here
> MyInt test=0
>
> BOOST_LIB_VERSION=1_48
> LUA_VERSION_NUM=501
> LUABIND_VERSION=900
> G++ VERSION=4.4.6 20110731 (Red Hat 4.4.6-3)
>
> Many thanks
> Piers
>
>
> ------------------------------------------------------------------------------
> Master Visual Studio, SharePoint, SQL, ASP.NET, C# 2012, HTML5, CSS,
> MVC, Windows 8 Apps, JavaScript and much more. Keep your skills current
> with LearnDevNow - 3,200 step-by-step video tutorials by Microsoft
> MVPs and experts. ON SALE this month only -- learn more at:
> http://p.sf.net/sfu/learnmore_122712
> _______________________________________________
> luabind-user mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/luabind-user
>

I do not know if Luabind tries to do something funky yet this is not
possible with Lua 5.1 or 5.2 as the types are different and they also
have different metatables,[1][2]

[1] http://www.lua.org/manual/5.1/manual.html#2.8
[2] http://www.lua.org/manual/5.2/manual.html#2.4

------------------------------------------------------------------------------
Master Visual Studio, SharePoint, SQL, ASP.NET, C# 2012, HTML5, CSS,
MVC, Windows 8 Apps, JavaScript and much more. Keep your skills current
with LearnDevNow - 3,200 step-by-step video tutorials by Microsoft
MVPs and experts. ON SALE this month only -- learn more at:
http://p.sf.net/sfu/learnmore_122712
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user