|
From: Gerhard Menzl on 11 Apr 2008 02:50 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! ]
|
Pages: 1 Prev: anonymous enums and templates Next: shift operator undefined behavior |