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 > Does taking add...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 5 Topic 9492 of 9828
Post > Topic >>

Does taking address of function template specialization not force instantiation?

by Gerhard Menzl <clcppm-poster@[EMAIL PROTECTED] > Apr 11, 2008 at 11:50 AM

Consider:

     #include <algorithm>
     #include <vector>

     template <typename Element,
               typename Member,
               Member (Element::*memPtr)()>
     bool lessMember(Element const& left, Element const& right)
     {
         return (left.*memPtr)() < (right.*memPtr)();
     }

     class C
     {
     public:
         C(int i, long l) : m_i(i), m_l(l) {}

         int  GetInt() { return m_i; }
         long GetLong() { return m_l; }

     private:
         int  m_i;
         long m_l;
     };

     int main()
     {
         std::vector<C> vc;

         static int const elementCount = 3;

         for (int elem = 0; elem < elementCount; ++elem)
             vc.push_back(C(elem, elementCount - elem));

// linker error without the following line uncommented
//        bool b = lessMember<C, long, &C::GetLong>(vc[0], vc[1]);

         std::sort(vc.begin(),
                   vc.end(),
                   lessMember<C, long, &C::GetLong>);
     }

Unless the function template specialization lessMember<C, long,
&C::GetLong> is called explicitly, my compiler (Visual C++ 6.0) does not
instantiate the template and causes a linker error (unresolved
external). Just taking the address of the specialization in the call to
std::sort does not seem to suffice. Is this one of the many limitations
of this outdated compiler, or does the Standard really not require an
instantiation here?

According to 14.7.1/2, a "function template specialization is implicitly
instantiated when the specialization is referenced in a context that
requires a function definition to exist". I would have assumed that
taking the address of a function (template specialization) requires its
definition.

In case instantiation is not required: is there any way to force
instantiation without an explicit specialization (in the actual code,
the function template is called from with in another function template),
and without inserting a dummy call for every specialization?

-- 
Gerhard Menzl

Non-spammers may respond to my email address, which is composed of my
full name, separated by a dot, followed by at, followed by "fwz",
followed by a dot, followed by "aero".



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




 5 Posts in Topic:
Does taking address of function template specialization not forc
Gerhard Menzl <clcppm-  2008-04-11 11:50:18 
Re: Does taking address of function template specialization not
=?ISO-8859-1?Q?Daniel_Kr=  2008-04-11 18:09:52 
Re: Does taking address of function template specialization not
Gerhard Menzl <clcppm-  2008-04-14 10:35:27 
Re: Does taking address of function template specialization not
Greg Herlihy <greghe@[  2008-04-14 13:13:15 
Re: Does taking address of function template specialization not
=?ISO-8859-1?Q?Daniel_Kr=  2008-04-14 21:23:27 

Post A Reply:
  Go here to Signup

AddThis Feed Button


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

Contact
tan12V112 Thu Jul 24 15:45:10 CDT 2008.