Yepessin / slb

Automatically exported from code.google.com/p/slb
0 stars 0 forks source link

unable to call a const std::string foo() const; method. #10

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. create a class with a const method with a const return type
2. define wrapper for the class
3. const_set("method", &Class::method)

What is the expected output? What do you see instead?
being able to call the method from lua.

What version of the product are you using? On what operating system?
cloned from hg.

Please provide any additional information below.

Original issue reported on code.google.com by serge.ra...@gmail.com on 20 Aug 2011 at 12:00

GoogleCodeExporter commented 8 years ago
here's the c++ code

class TObject {
public:
     TObject() {
     }

     ~TObject() {
     }

    void setInt(int pInt) { mInt = pInt; }
    const int getInt() const { return mInt; }

private:                                                                        

    int mInt;
};                                                                              

void wrapClasses()
{                                                                               

    SLB::Class<TObject>("TObject")
        .constructor()
        .set("setInt", &TObject::setInt)
            .param("value for i")
        .const_set("getInt", &TObject::getInt);
}

here's the lua code

t1 = TObject()
t1:setInt(5)
val = t1:getInt()

and here's the error message

----------------------------------
Lua Error:
        test.lua:3: Unknown class i
Traceback:
         [ 0 (C) ] 
         [ 1 (C) ]  @ getInt(method)
         [ 2 (main) ] hello.lua:3

Original comment by serge.ra...@gmail.com on 20 Aug 2011 at 11:06

GoogleCodeExporter commented 8 years ago
well it works if i change the return type to be const reference, as in

    const std::string&

is that some limitation in Lua/C++ (Templates)?

Original comment by serge.ra...@gmail.com on 21 Aug 2011 at 5:02

GoogleCodeExporter commented 8 years ago
Indeed, is a problem with the template specialization, it doesn't get along 
with "const type" , Can you be more detailed about why you need a const string, 
and not a const string& or just string ?

As far as I know const string will make a copy of the string but the temporary 
variable created to hold the string will be inmutable, but I don't see any 
performance gain or advantage over a plain string.

Original comment by joseLuis...@gmail.com on 5 Sep 2011 at 9:04

GoogleCodeExporter commented 8 years ago
not a big deal for me so far, however, being forced to return a reference would 
mean to keep a valid instance of that particular object around, for the 
reference to stay valid.

Original comment by serge.ra...@gmail.com on 6 Sep 2011 at 6:32

GoogleCodeExporter commented 8 years ago
You can also not return a reference but a copy leaving the const:

int mymethod(...) instead of const int mymethod(...) or
string mymethod(...) instead of const string mymethod(...) 

I don't see any advantage in returning a const COPY, but if you really need 
this I can try to figure out if it's possible to do it. 

Original comment by joseLuis...@gmail.com on 6 Sep 2011 at 7:11

GoogleCodeExporter commented 8 years ago
yeah, true.

no, as i said, no big of a deal :) ... i just wanted to know why it wouldn't 
work. thanks for the answers.

Original comment by serge.ra...@gmail.com on 6 Sep 2011 at 7:15

GoogleCodeExporter commented 8 years ago
It's a problem with the template matching mechanism SLB relies on, type  "int" 
it's almost indistinguishable from "const int" at that level. :)  

Original comment by joseLuis...@gmail.com on 6 Sep 2011 at 7:20