c++ - Argument conversion in the presence of function templates -
i have set of function templates depend on 2 integers. these need instantiated, except 1 of function arguments needs converted. conversion operator provided purpose. compiler (vs 2013 express preview) refuses recognize possibility of conversion. see code below.
in conversion_op.h
template<int n> class proxy { }; template<int m, int n> void func(const proxy<m>& p1, const proxy<n>& p2) { } typedef proxy<1> specialproxy; class orig { specialproxy p; public: operator specialproxy() const { return p; } };
in conversion_op.cpp
#include "conversion_op.h" //template void func(const specialproxy&, const specialproxy&); //template void func(const specialproxy&, const proxy<2>&); //template void func(const proxy<2>&, const specialproxy&); //template void func(const proxy<2>&, const proxy<2>&); int test() { orig a; specialproxy p1; proxy<2> p2; func<1,1>(a, p1); //ok func<1,2>(a, p2); //ok func<1,1>(p1, a); //ok func<2,1>(p2, a); //ok func(a, p1); //error func(a, p2); //error func(p1, a); //error func(p2, a); //error }
the error is: "could not deduce template argument 'const proxy &' 'orig'. please see declaration of func."
i tried replacing
operator specialproxy() const;
by
operator const specialproxy&() const;
but error remains same.
also tried explicitly instantiating needed function (commented out code in conversion_op.cpp), no luck.
any insight appreciated. thank you.
the problem here c++ not allow implicit conversion when deducing template arguments.
when compiler tries instantiate func(a, p1)
instance, can promptly deduce n -> 1, has no value m, nor allowed deduce out of implicit conversion of a
proxy<1>
defined in orig
.
Comments
Post a Comment