Compile error: invalid initialization of non-const reference of type ‘some_slot&’ from a temporary of type ...

View: New views
4 Messages — Rating Filter:   Alert me  

Compile error: invalid initialization of non-const reference of type ‘some_slot&’ from a temporary of type ...

by Peter Basista :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Some parts of this message have been removed. Learn more about Nabble's security policy.
Hello,

I am having problem with compilation of the following code:

class ExampleClass {
public:
        int f (int i, double d, char c);
        int g (int i, double d, char c, char * s);
};

int ExampleClass::f (int i, double d, char c) {
        std::cout << "Executing ExampleClass::f." << std::endl;
        return (0);
}

int ExampleClass::g (int i, double d, char c, char * s) {
        std::cout << "Executing ExampleClass::g." << std::endl;
        return (0);
}

typedef sigc::slot<int, int, double, char, char *> gslot;

int bar (gslot & gs) { // The argument is a reference
        std::cout << "Executing bar." << std::endl;
        gs(0, 1.0, 'c', "hello");
        return (0);
}

int bar2 (gslot gs) { // The reference is missing here
        std::cout << "Executing bar2." << std::endl;
        gs(0, 1.0, 'c', "hello");
        return (0);
}

int foo () {
        ExampleClass ec;
        gslot intermediate = sigc::hide(sigc::mem_fun(ec, &ExampleClass::f));
        bar(intermediate); // works
        bar(sigc::hide(sigc::mem_fun(ec, &ExampleClass::f))); // <-- COMPILE ERROR
        bar2(sigc::hide(sigc::mem_fun(ec, &ExampleClass::f))); // works
        return (0);
}

On my system with:

gcc (GCC) 4.1.2 (Gentoo 4.1.2 p1.3)
sigc++-2.0 version 2.2.3

I get this compile error:

error: invalid initialization of non-const reference of type ‘gslot&’ from a temporary of type ‘sigc::hide_functor<-0x00000000000000001, sigc::bound_mem_functor3<int, ExampleClass, int, double, char> >’
error: in passing argument 1 of ‘int bar(gslot&)’

This is interesting, because when I use an intermediate variable of the type sigc::slot to which I at first assign the created functor, then the parameter is passed successfully. Also, when I use a function (bar2) which is not taking a reference to sigc::slot, but just a sigc::slot as a parameter, then I am able to call it with the created functor directly.

I don't really understand this behaviour. What is the difference between calls to bar and bar2? Why is it not possible to initialize a parameter of type 'reference to sigc::slot' with a functor returned by sigc::hide directly? Why it is necessary to use intermediate sigc::slot? Should not be such a conversion automatic?

Thank you very much!


What can you do with the new Windows Live? Find out
_______________________________________________
libsigc-list mailing list
libsigc-list@...
http://mail.gnome.org/mailman/listinfo/libsigc-list

Re: Compile error: invalid initialization of non-const reference of type ‘some_slot&’ from a temporary of type ...

by James Lin-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Peter Basista wrote:

>
> int bar (gslot & gs) { // The argument is a reference
>         std::cout << "Executing bar." << std::endl;
>         gs(0, 1.0, 'c', "hello");
>         return (0);
> }
>
> [...]
>
>         bar(sigc::hide(sigc::mem_fun(ec, &ExampleClass::f)));
> // <-- COMPILE ERROR

C++ does not allow anonymous objects to be passed where non-const references are expected.  Specifically, non-const references must be lvalues.

http://groups.google.com/group/comp.lang.c++/browse_thread/thread/814a071947c5b675/be95a615245cdbdf?#be95a615245cdbdf

- James
_______________________________________________
libsigc-list mailing list
libsigc-list@...
http://mail.gnome.org/mailman/listinfo/libsigc-list

Re: Compile error: invalid initialization of non-const reference of type ‘some_slot&’ from a temporary of type ...

by Peter Basista :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message



_________________________________________________________________
Windows Live™: Keep your life in sync. Check it out!
http://windowslive.com/explore?ocid=TXT_TAGLM_WL_t1_allup_explore_012009
_______________________________________________
libsigc-list mailing list
libsigc-list@...
http://mail.gnome.org/mailman/listinfo/libsigc-list

Parent Message unknown Re: Compile error: invalid initialization of non-const reference of type ‘some_slot&’ from a temporary of type ...

by Peter Basista :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Some parts of this message have been removed. Learn more about Nabble's security policy.
I am sorry for the previous empty message, I pressed the wrong button ...
-----------------------------------------------------------------------

James Lin wrote:
> C++ does not allow anonymous objects to be passed where non-const references are expected. Specifically, non-const references must be lvalues.
>
> http://groups.google.com/group/comp.lang.c++/browse_thread/thread/814a071947c5b675/be95a615245cdbdf?#be95a615245cdbdf

Well, I hope I see what you mean. I was not aware of this. After I corrected the definition of bar and added const:

int bar (const gslot & gs)

The code compiled without problems.

Thank you very much, James!


See all the ways you can stay connected to friends and family
_______________________________________________
libsigc-list mailing list
libsigc-list@...
http://mail.gnome.org/mailman/listinfo/libsigc-list