Re: [sigc] function of const object as slot



Ulrich Eckhardt wrote:
Roel Vanhout wrote:
 > The program below doesn't compile (at least on MSVC, the error is:
Naming a compiler without a version is almost meaningless. Also it would be
nice to know that you're using sigc++ 1.x. ;)

Yeah you're right of course, the compiler was msvc .net 2003. And it was indeed 1.5, I didn't mention because it didn't even occur to me that that might make a difference ;)

 > error C2440:
 > 'initializing' : cannot convert from 'const Foo' to 'Foo &'
 >          Conversion loses qualifiers
 > see reference to
 > function template instantiation
 > 'SigC::Slot0<R>
 >  SigC::slot<std::string,const Foo,Foo>(O1 &,R (__thiscall Foo::* )(void)
 > const)' being compiled
 >          with
 >          [
 >              R=std::string,
 >              O1=const Foo
 >          ]
 > )
 >
 > How can I connect to a slot of a const object?
You can't. The point is that connecting a  SigC::Object is a mutating
operation on the SigC::Object underneath - your connection is registered
there, so it can be automatically disconnected when the SigC::Object is
destroyed.

Aha yes indeed I see now.

 > I've tried putting
 > 'const' just about everywhere I could imagine but no change apart from
 > other error messages :) It compiles ok when I don't make the object 'f'
 > const. Is it possible at all?
Ideas:
- Use const_cast, but that invokes UB when the object is a constant, i.e. if
it isn't just that you only have a const reference to it.
- Use SigC::class_slot, but then you loose the automatic disconnecting and
need to implement that manually - sometimes the automatic one doesn't work
that good anyway so it might not be a problem.
- Don't use a const object.

Well to be honest I don't need it myself, I've never bothered with const-correctness (that may explain why I didn't see what the problem was in the first place), I got this question as a reply to my article on codeproject on libsigc++ (http://www.codeproject.com/cpp/sigcart.asp?msg=1201497#xx1201497xx) and I couldn't figure it out myself. I'll post a pointer to this message in response to that question :) Thanks (thanks to Arnaud for replying as well of course).

cheers,

roel




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]