c++ - Failing the template function lookup -
consider following example.
#include <iostream> #include <boost/optional.hpp> template < typename > int boo( const boost::optional< > &a ); template < typename > int foo( const &a ) { return boo( ); } template < typename > int boo( const boost::optional< > & ) { return 3; } int main() { std::cout << "foo = " << foo( 3 ) << std::endl; std::cout << "boo = " << boo( 3 ) << std::endl; }
compiling using g++ 4.3.0 throws next compiling errors:
dfg.cpp: in function ‘int main()’: dfg.cpp:25: error: no matching function call ‘boo(int)’ dfg.cpp: in function ‘int foo(const a&) [with = int]’: dfg.cpp:24: instantiated here dfg.cpp:12: error: no matching function call ‘boo(const int&)’
what should differently (if possible references c++ standard)? why happening , how fix it?
edit
the fix create correct type in foo
:
template < typename > int foo( const &a ) { const boost::optional< > opta( ); return boo( opta ); }
but questions still stands: why not created automatically?
return boo( );
here type of a
int
, , there no function name boo
accepts argument of type int
. hence see error:
dfg.cpp:25: error: no matching function call ‘boo(int)’
even if int
can implicitly converted boost::optional<int>
, compiler cannot deduce template argument boost::optional<t>
calling site. it's 1 of non-deduced contexts explicitly need mention type as,
return boo<a>(a);
the standard says in $14.8.2.1,
if template-parameter not used in of function parameters of function template, or used in non-deduced context, its corresponding template-argument cannot deduced function call , template-argument must explicitly specified.
Comments
Post a Comment