Consider following example.
#include <iostream>
#include <boost/optional.hpp>
template < typename A >
int boo( const boost::optional< A > &a );
template < typename A >
int foo( const A &a )
{
return boo( a );
}
template < typename A >
int boo( const boost::optional< A > & )
{
return 3;
}
int main()
{
std::cout << "foo = " << foo( 3 ) << std::endl;
std::cout << "boo = " << boo( 3 ) << std::endl;
}
Compiling this using g++ 4.3.0 throws the next compiling errors:
dfg.cpp: In function ‘int main()’:
dfg.cpp:25: error: no matching function for call to ‘boo(int)’
dfg.cpp: In function ‘int foo(const A&) [with A = int]’:
dfg.cpp:24: instantiated from here
dfg.cpp:12: error: no matching function for call to ‘boo(const int&)’
What should I do differently (if possible with references from the C++ standard)?
Why is it happening and how do I fix it?
EDIT
The fix is to create the correct type in foo:
template < typename A >
int foo( const A &a )
{
const boost::optional< A > optA( a );
return boo( optA );
}
But the questions still stands: why is it not created automatically?
Here type of
aisint, and there is no function with nameboowhich accepts argument of typeint. Hence you see this error:Even if
intcan be implicitly converted intoboost::optional<int>, the compiler cannot deduce the template argument forboost::optional<T>from the calling site. It’s one of the non-deduced contexts where you explicitly need to mention the type as,The Standard says in $14.8.2.1,