Re: [boost] Encapsulate boost::bind in a template method
Hi! I can't make SetEventHandler to a free/global function because the EventFunction that boost::bind created should be added to a private std::map of CWindow. Or I need an extra member method that only adds the boost::function to the map. But it now works with a static_cast in SetEventHandler how Sam suggested. :) At the moment I am contented with the interface of SetEventHandler, but maybe I have to change it later. Time will reveal. ;-) Thanks Peter, thanks Douglas. cu, Matthias - Original Message - From: Peter Dimov [EMAIL PROTECTED] To: Boost mailing list [EMAIL PROTECTED] Sent: Wednesday, February 19, 2003 3:37 PM Subject: Re: [boost] Encapsulate boost::bind in a template method Matthias Hoffrichter wrote: Hi, I want to encapsulate boost::find in a template method in a base class for easier use. Here is some code: #include boost/function.hpp #include boost/bind.hpp class CWindow { public: CWindow() { SetEventHandler(CWindow::OnCreate); // this call works } long OnCreate() { return 0; } templatetypename T void SetEventHandler(long (T::*Function)()) { boost::functionlong EventFunction = boost::bind(Function, this); // ... // Add EventFunction into a std::map } }; class CButton : public CWindow { public: CButton() { SetEventHandler(CButton::OnPaint); // this call doesn't compile } long OnPaint() { return 0; } }; In CWindow::SetEventHandlerCButton, 'this' is a CWindow*. You can't invoke CButton::OnPaint using a pointer to CWindow. The easiest solution is probably to make SetEventHandler a free function: templateclass F, class T void setEventHandler(F f, T * p) { boost::functionlong EventFunction = boost::bind(f, p); // ... // Add EventFunction into a std::map } and then simply use setEventHandler(CButton::OnPaint, this) in CButton::CButton(). You'll soon find that this is not _that_ easier to use compared to setEventHandler(bind(CButton::OnPaint, this)); and the latter is much more flexible: setEventHandler(bind(CButton::OnMessage, this, WM_PAINT)); but that's another story. :-) ___ Unsubscribe other changes: http://lists.boost.org/mailman/listinfo.cgi/boost ___ Unsubscribe other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Re: [boost] shared_ptr extension with counter in object header
At 03:34 AM 2/23/2003, Thomas Witt wrote: Greg, I can not figure out how you make sure to satisfy the alignment constraints of T and counted_base_header_impl. Any hint would be appreciated. I just used two char arrays, thinking there was a dispensation somewhere in the standard to allow for placement new into char arrays: templatetypename T struct object_with_counted_header { char header[sizeof(counted_base_header_implT*,checked_deleterT )]; char object[sizeof(T)]; }; If I'm wrong, an alternative might be: templatetypename T struct object_with_counted_header { counted_base_header_implT*,checked_deleterT header; T object; }; But then you can't use offsetof, and some fancier tricks will be in order. ___ Unsubscribe other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Re: [boost] More metaprogramming problems with MSVC7.0
Andreas Huber [EMAIL PROTECTED] writes: Hi Aleksey all other metaprogramming gurus The attached code compiles just fine with MSVC7.1 but MSVC7.0 once more has its problems. This time I'm quite sure it has nothing to do with MPL, instead VC7.0 seems to get confused and reports the following: d:\Data\StopWatch\StopWatch.cpp(58) : error C2516: 'state_base_typeDerived,Context,Transitions,InnerInitial::type' : is not a legal base class According to the docs this error is reported when you try to inherit from built-in types like int. If you then remove the 3 first characters of the lines marked with // *** here ***, the program compiles and you can see in the debugger that the result returned by the metafunction state_base_type is quite a legal base class (see type of pWhatever). Has anyone else ever encountered similar problems with either 7.0 or 6.5? Are there any workarounds? This looks seriously like ETI (early template instantiation) again. Unfortunately I'm not sure exactly what the fix is; Aleksey is the only person I know who really understands how to work around this. One thing you could try: template class Derived, class Context, class Transitions = empty_list, class InnerInitial = empty_list class simple_state : public mpl::identity // Here typename state_base_type Derived, Context, Transitions, InnerInitial ::type ::type// Here {}; HTH, -- Dave Abrahams Boost Consulting www.boost-consulting.com ___ Unsubscribe other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
[boost] Re: More metaprogramming problems with MSVC7.0
Dave, David Abrahams [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] This looks seriously like ETI (early template instantiation) again. Unfortunately I'm not sure exactly what the fix is; Aleksey is the only person I know who really understands how to work around this. One thing you could try: template class Derived, class Context, class Transitions = empty_list, class InnerInitial = empty_list class simple_state : public mpl::identity // Here typename state_base_type Derived, Context, Transitions, InnerInitial ::type ::type// Here {}; Unfortunately this doesn't help, 7.0 reports d:\Data\StopWatch\StopWatch.cpp(58) : error C2516: 'boost::mpl::identityT::type' : is not a legal base class with [ T=state_base_typeDerived,Context,Transitions,InnerInitial::type ] But thanks anyway! I'll wait for Aleksey then... Regards, Andreas ___ Unsubscribe other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
[boost] Re: Re: More metaprogramming problems with MSVC7.0
One last shot-in-the-dark: add this specialization: template class simple_stateint, int, int, int {}; What ETI does is to use an instantiation of some template with all int parameters internally as a stopgap measure while it's waiting to discover what the parameters really are. After this, I give up! No luck either. Compilation fails with exactly the same error. Thanks again, Andreas ___ Unsubscribe other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Re: [boost] Re: More metaprogramming problems with MSVC7.0
Andreas Huber wrote: Hi Aleksey all other metaprogramming gurus Hi Andreas, The attached code compiles just fine with MSVC7.1 but MSVC7.0 once more has its problems. This time I'm quite sure it has nothing to do with MPL, instead VC7.0 seems to get confused and reports the following: d:\Data\StopWatch\StopWatch.cpp(58) : error C2516: 'state_base_typeDerived,Context,Transitions,InnerInitial::type' : is not a legal base class According to the docs this error is reported when you try to inherit from built-in types like int. If you then remove the 3 first characters of the lines marked with // *** here ***, the program compiles and you can see in the debugger that the result returned by the metafunction state_base_type is quite a legal base class (see type of pWhatever). Has anyone else ever encountered similar problems with either 7.0 or 6.5? Yep, it's a known bug called early template instantiation (ETI). It's briefly described here - http://lists.boost.org/MailArchives/boost/msg39915.php. Are there any workarounds? Sure. In your case, it would be as simple as this: #include boost/mpl/aux_/msvc_eti_base.hpp ... template class Derived, class Context, class Transitions = empty_list, class InnerInitial = empty_list class simple_state : public mpl::aux::msvc_eti_base typename state_base_type // Derived, Context, Transitions, InnerInitial ::type ::type {}; You will need the latest CVS for that, though. Aleksey ___ Unsubscribe other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Re: [boost] Re: Thread-Local Storage (TLS) and templates
David Abrahams [EMAIL PROTECTED] writes: | Alexander Terekhov [EMAIL PROTECTED] writes: | | David Abrahams [EMAIL PROTECTED] writes: | | | I disagree with your conclusion. As I've said elsewhere, k can be a | | compile-time constant in the same way that X::k is a compile-time | | constant. | | Certainly, you've said that. But that assertion by itself does not | constitute a proof of the well-foundness of the attempted analogy or | whether the analogy actually constitutes a proof. | | It's not intended to be proof in the mathematical sense; I doubt I | have the energy for that ;-), though I think MSVC probably constitutes | an existence proof. | | Yeah. Indeed: http://tinyurl.com/673e | | OK, whatever. I guess I should've said that you can instantiate a | template on k with sensible results. Whether or not you want to call | it a constant is another semantic matter. I'd call it a constant | which evaluates differently in different threads. ^^ And that isn't just a word-game as your message might imply it. | Within a single thread the value never changes. Like in templatetypename T struct X { }; int main() { const int i = 2003; Xi x; } ? In a given program run, i won't change. That is just in single thread mode. Let's solve that first. Extrapolating to a multi-thread mode isn't just changing a word. -- Gaby ___ Unsubscribe other changes: http://lists.boost.org/mailman/listinfo.cgi/boost