考虑以下示例。
#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;
}
使用g++4.3.0编译会引发下一个编译错误:
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?
编辑
修复方法是在<code>foo</code>中创建正确的类型:
template < typename A >
int foo( const A &a )
{
const boost::optional< A > optA( a );
return boo( optA );
}
但问题仍然存在:为什么它不是自动创建的?