Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ Moderated > Re: ADL woes
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 4 of 7 Topic 9510 of 9830
Post > Topic >>

Re: ADL woes

by Kimon Hoffmann <kimon.hoffmann@[EMAIL PROTECTED] > Apr 18, 2008 at 03:01 PM

Hi all,

First of all thanks to Alberto Ganesh Barbati for being so kind to
explain the binding in various phases.
After leaving the code alone for one day, a solution occurred to me,
that is not really pretty, but at least it is working as expected.
Here is the revised code:

----------------------------------------------------------------------

// Code included from DefaultImplementation.hpp

namespace default_impl {

   struct DefaultType { /* ... */ };

   template<typename T>
   void func(T const&);

   template<>
   inline void func(DefaultType const&) {
     // .. do something ...
   }

}

// Code included from Algorithm.hpp

namespace algorithm {

   template<typename T>
   void cool_algorithm_1(T const& value) {
     using default_impl::func;
     // ...
     func(value);
     // ...
   }

   template<typename T>
   void cool_algorithm_2(T const& value) {
     // ...
     default_impl::func(value);
     // ...
   }

}

// Code included from Working.hpp

struct Working { /* ... */ };

namespace default_impl {
	
   template<>
   inline void func(Working const&) {
     // .. do something else ...
   }

}

// Code from Main.cpp
int main(int, char**) {

   Working working;
   algorithm::cool_algorithm_1(working);
   algorithm::cool_algorithm_2(working);

   return 0;
}

----------------------------------------------------------------------

My understanding of why this works as expected is that both calls to
func() bind to the primary template forward declaration of func(),
because "Specializations don't overload" (see [1]).
At link time the individual (implicitly specialized) calls to func()
resolve to the explicitly specialized versions of the function and the
code links flawlessly.

As I said in the introduction, this solution is not very pretty. The
first reason is the somewhat unintuitive semantics of function template
specializations (again "Specializations don't overload"), as explained
by Herb Sutter in his article "Why Not Specialize Function Templates?"
[1], and the second reason is that missing specializations are only
detected at link time and thus don't produce compiler errors as one
would expect, but rather linker errors that might be hard to track down.

If I have some time to spare I'll probably restructure the whole thing
to make use of the nice workaround mentioned in the abovementioned
article.

Best regards,
   Kimon

[1] http://www.gotw.ca/publications/mill17.htm

-- 
      [ See http://www.gotw.ca/resources/clcm.htm
for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
 




 7 Posts in Topic:
ADL woes
Kimon Hoffmann <kimon.  2008-04-16 15:45:27 
Re: ADL woes
Alberto Ganesh Barbati &l  2008-04-17 03:51:35 
Re: ADL woes
Alberto Ganesh Barbati &l  2008-04-17 16:43:14 
Re: ADL woes
Kimon Hoffmann <kimon.  2008-04-18 15:01:58 
Re: ADL woes
Kimon Hoffmann <kimon.  2008-04-18 15:02:05 
Re: ADL woes
Jiri Palecek <jpalecek  2008-04-19 01:39:31 
Re: ADL woes
Kimon Hoffmann <kimon.  2008-04-19 18:11:06 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Jul 25 15:26:56 CDT 2008.