|
Boost Users : |
Subject: [Boost-users] enable-if overloading out the wazoo
From: John Dlugosz (JDlugosz_at_[hidden])
Date: 2010-04-23 16:31:39
I tried something like this: First, the general form in one header known by everything:
template <typename T1, typename T2>
dim_s foo (const T1& left, const T2& right);
Then, in a header that gets more concrete, provide its own form that is meant to be used when objects of those types are involved:
template<typename T1, typename T2>
typename enable_if_c<
is_base_of<genericbase,T1>::value &&
is_base_of<genericbase,T2>::value,
dim_s>::type foo (const T1& left, const T2& right);
Here, the template defined in this header derives from genericbase, so I have a hook to say "all types derived from any specialization of this template".
Now, this actually causes an ambiguity. The latter is not a specialization but a peer overloading, and when both are present, both are templates that form exact matches and template arguments at the least restrictive way to refer to the type.
This function is already internal plumbing, so I don't mind it being a little messy to use. What I'm really doing is multimethod dispatch: various concrete classes, generic versions for making such, and special combinations all have their own efficient logic, with a fallback (slow) that always works. And I want all the plumbing to disappear at compile time so it efficiently calls the efficient function. So if I'm really guilty of the "X for Y" problem, that is what I'm really trying to do, and if there are any suggestions on doing that better in general, please let'em rip.
As for the road I'm on now, the simplest thing is to somehow give each form a priority. I did that by adding a 3rd parameter. The general form declares a void*, and the specialized form declares an int. The caller always gives a zero. I'm wondering what is the longest chain of different priorities I can use with this method, using only compiler primitive types that the optimizer should be able to toss when the function is inlined. I know I could use dummy classes with any number of derivations, but I'm thinking I can get several levels just using primitive types. Any suggestions?
Thanks in advance,
--John
(watch the footer)
TradeStation Group, Inc. is a publicly-traded holding company (NASDAQ GS: TRAD) of three operating subsidiaries, TradeStation Securities, Inc. (Member NYSE, FINRA, SIPC and NFA), TradeStation Technologies, Inc., a trading software and subscription company, and TradeStation Europe Limited, a United Kingdom, FSA-authorized introducing brokerage firm. None of these companies provides trading or investment advice, recommendations or endorsements of any kind. The information transmitted is intended only for the person or entity to which it is addressed and may contain confidential and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon, this information by persons or entities other than the intended recipient is prohibited. If you received this in error, please contact the sender and delete the material from any computer.
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net