enable-if overloading out the wazoo
I tried something like this: First, the general form in one header known by everything:
template
On Fri, 23 Apr 2010, John Dlugosz wrote:
I tried something like this: First, the general form in one header known by everything:
template
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 enable_if_c< is_base_of ::value && is_base_of ::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.
Have you seen URL:http://www.codeproject.com/KB/recipes/mmcppfcs.aspx?
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?
What about (... is for you to fill in): template <..., typename X> // Priority 0 void foo(..., X); template <..., typename X> // Priority 1 void foo(..., X*); template <..., typename X> // Priority 2 void foo(..., X**); Then call foo(..., (void*******************)0) (for however many priority levels you want to allow). Perhaps you can even use a default argument to fill in the value in C++0x, but I'm not sure about that. You can of course write a wrapper function that adds in the dummy last argument. -- Jeremiah Willcock
John Dlugosz wrote:
I tried something like this: First, the general form in one header known by everything:
template
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 enable_if_c< is_base_of ::value && is_base_of ::value, dim_s>::type foo (const T1& left, const T2& right);
Isn't that causing ODR violation ? the enable_if'ed foo should also present in the header IMHO -- ___________________________________________ Joel Falcou - Assistant Professor PARALL Team - LRI - Universite Paris Sud XI Tel : (+33)1 69 15 66 35
template
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 enable_if_c< is_base_of ::value && is_base_of ::value, dim_s>::type foo (const T1& left, const T2& right); Isn't that causing ODR violation ? the enable_if'ed foo should also present in the header IMHO
-- ___________________________________________ Joel Falcou - Assistant Professor
I'm assuming that you won't have any objects of the types being enabled unless that header is included. I know that the compiler needs to be aware of explicit specializations before making a call that would use one. But this isn't a specialization anyway, but an overloaded foo. I see that overloading would not be allowed if it differs in the return type only. But I was under the impression that for templates they are indeed different templates. Now there is no way to distinguish between them as written, when is an extension of the issue I was asking about. I could put the enable_if in a dummy extra argument that has a default value -- that would prevent the signatures from being the same, and it would not affect my question. --John 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.
----- Original Message -----
From: "John Dlugosz"
I tried something like this: First, the general form in one header known by everything:
template
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 enable_if_c< is_base_of ::value && is_base_of ::value, dim_s>::type foo (const T1& left, const T2& right);
What about overloading with the genericbase class? dim_s foo (const genericbase& left, const genericbase& right); Am I missing something? Best, Vicente
-----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users- bounces@lists.boost.org] On Behalf Of vicente.botet Sent: Monday, April 26, 2010 1:26 AM To: boost-users@lists.boost.org Subject: Re: [Boost-users] enable-if overloading out the wazoo
----- Original Message ----- From: "John Dlugosz"
To: Sent: Friday, April 23, 2010 10:31 PM Subject: [Boost-users] enable-if overloading out the wazoo I tried something like this: First, the general form in one header
known by everything:
template
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 enable_if_c< is_base_of ::value && is_base_of ::value, dim_s>::type foo (const T1& left, const T2& right); What about overloading with the genericbase class?
dim_s foo (const genericbase& left, const genericbase& right);
Am I missing something?
Best, Vicente
The plain overloaded function will be a worse match than the first template form I showed, which is always an exact match for all types. It won't get used. --John 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.
participants (4)
-
Jeremiah Willcock
-
joel falcou
-
John Dlugosz
-
vicente.botet