c++ - partial ordering of template functions
-
Christof Meerwald
(37/37)
Dec 23 2002
#include
- Walter (5/42) Dec 31 2002 I've fixed the first issue, 'Error: 'f' previously declared as something
- Christof Meerwald (24/58) Dec 31 2002 See 14.8.3 Overload resolution [temp.over]
- Walter (6/64) Dec 31 2002 All right. That does make some sense. I'll check it out. -Walter
#include <stdio.h> template<class T> struct A { }; struct B { }; template<class U, class T> A<U> f(T t) { printf("1\n"); return A<U>(); } template<class T> A<B> f(T t) // Error: 'f' previously declared as something else // It was declared as: A<U>C func(T) // It is now declared: A<B>C func(T) { printf("2\n"); return A<B>(); } int main() { f(0); // Error: ambiguous reference to symbol f<int>(0); // Error: ambiguous reference to symbol return 0; } Extracted from Boost.Bind (to get adaptable function objects working). gcc-3.0 chooses the second function for "f(0)" and the first one for "f<int>(0)" and I guess that's what Boost.Bind expects. bye, Christof -- http://cmeerw.org JID: cmeerw jabber.at mailto cmeerw at web.de ...and what have you contributed to the Net?
Dec 23 2002
I've fixed the first issue, 'Error: 'f' previously declared as something else'. But I think the other two should be ambiguous, I cannot find a rule that gives the results you are seeing from gcc-3.0. "Christof Meerwald" <cmeerw web.de> wrote in message news:au7ab9$67p$1 digitaldaemon.com...#include <stdio.h> template<class T> struct A { }; struct B { }; template<class U, class T> A<U> f(T t) { printf("1\n"); return A<U>(); } template<class T> A<B> f(T t) // Error: 'f' previously declared as something else // It was declared as: A<U>C func(T) // It is now declared: A<B>C func(T) { printf("2\n"); return A<B>(); } int main() { f(0); // Error: ambiguous reference to symbol f<int>(0); // Error: ambiguous reference to symbol return 0; } Extracted from Boost.Bind (to get adaptable function objects working). gcc-3.0 chooses the second function for "f(0)" and the first one for "f<int>(0)" and I guess that's what Boost.Bind expects. bye, Christof -- http://cmeerw.org JID: cmeerw jabber.at mailto cmeerw at web.de ...and what have you contributed to the Net?
Dec 31 2002
On Tue, 31 Dec 2002 14:18:54 -0800, Walter wrote:I've fixed the first issue, 'Error: 'f' previously declared as something else'. But I think the other two should be ambiguous, I cannot find a rule that gives the results you are seeing from gcc-3.0. "Christof Meerwald" <cmeerw web.de> wrote in message news:au7ab9$67p$1 digitaldaemon.com...See 14.8.3 Overload resolution [temp.over] "If, for a given function template, argument deduction fails, no such function is added to the set of candidate functions for that template." And argument deduction should fail for "template<class U, class T> A<U> f(T t)" (because U can't be deduced), therefore it should not be added to the set of candidate functions leaving "template<class T> A<B> f(T t)" as the only possible candidate.#include <stdio.h> template<class T> struct A { }; struct B { }; template<class U, class T> A<U> f(T t) { printf("1\n"); return A<U>(); } template<class T> A<B> f(T t) { printf("2\n"); return A<B>(); } int main() { f(0); // Error: ambiguous reference to symbolSee 14.5.5.2 Partial ordering of function templates [temp.func.order], paragraphes 3 and 4. "template<class U, class T> A<U> f(T t)" is at least as specialized as "template<class T> A<B> f(T t)". But, "template<class T> A<B> f(T t)" is not at least as specialized as "template<class U, class T> A<U> f(T t)" (argument deduction fails because U can't be deduced from the function parameter list) Thus "template<class U, class T> A<U> f(T t)" is more specialized than "template<class T> A<B> f(T t)" and should therefore be selected. That's my interpretation of the standard... bye, Christof PS: Happy New Year! :-) -- http://cmeerw.org JID: cmeerw jabber.at mailto cmeerw at web.de ...and what have you contributed to the Net?f<int>(0); // Error: ambiguous reference to symbol
Dec 31 2002
All right. That does make some sense. I'll check it out. -Walter "Christof Meerwald" <cmeerw web.de> wrote in message news:autg6u$1e2h$1 digitaldaemon.com...On Tue, 31 Dec 2002 14:18:54 -0800, Walter wrote:ruleI've fixed the first issue, 'Error: 'f' previously declared as something else'. But I think the other two should be ambiguous, I cannot find af(Tthat gives the results you are seeing from gcc-3.0. "Christof Meerwald" <cmeerw web.de> wrote in message news:au7ab9$67p$1 digitaldaemon.com...See 14.8.3 Overload resolution [temp.over] "If, for a given function template, argument deduction fails, no such function is added to the set of candidate functions for that template." And argument deduction should fail for "template<class U, class T> A<U>#include <stdio.h> template<class T> struct A { }; struct B { }; template<class U, class T> A<U> f(T t) { printf("1\n"); return A<U>(); } template<class T> A<B> f(T t) { printf("2\n"); return A<B>(); } int main() { f(0); // Error: ambiguous reference to symbolt)" (because U can't be deduced), therefore it should not be added to the set of candidate functions leaving "template<class T> A<B> f(T t)" as the only possible candidate.USee 14.5.5.2 Partial ordering of function templates [temp.func.order], paragraphes 3 and 4. "template<class U, class T> A<U> f(T t)" is at least as specialized as "template<class T> A<B> f(T t)". But, "template<class T> A<B> f(T t)" is not at least as specialized as "template<class U, class T> A<U> f(T t)" (argument deduction fails becausef<int>(0); // Error: ambiguous reference to symbolcan't be deduced from the function parameter list) Thus "template<class U, class T> A<U> f(T t)" is more specialized than "template<class T> A<B> f(T t)" and should therefore be selected. That's my interpretation of the standard... bye, Christof PS: Happy New Year! :-) -- http://cmeerw.org JID: cmeerw jabber.at mailto cmeerw at web.de ...and what have you contributed to the Net?
Dec 31 2002