【发布时间】:2015-10-12 08:01:46
【问题描述】:
我遇到了一个问题,gcc 4.9.2(使用 -std=c++11)没有编译一段代码,错误消息为
重载 'InsertDataIntoInputMap(int&, boost::shared_ptr&)' 的调用不明确
代码确实使用 msvc 2013 编译
#include <iostream>
#include <map>
#include <boost/shared_ptr.hpp>
struct Proxy
{
typedef std::map<int, int> InputDataMap;
int a;
};
template<class C, class D>
void InsertDataIntoInputMap(
const typename C::InputDataMap::key_type& key,
const D val)
{
std::cout << "Not shared\n";
}
template<class C, class D>
void InsertDataIntoInputMap(
const typename C::InputDataMap::key_type& key,
const boost::shared_ptr<D> val)
{
if (val)
{
std::cout << "Shared\n";
}
}
int main() {
int a;
boost::shared_ptr<double> x(new double(4.5));
InsertDataIntoInputMap<Proxy>(a, x);
}
虽然以下内容确实可以同时使用 gcc 和 msvc 进行编译:
#include <iostream>
#include <boost/shared_ptr.hpp>
template<class C, class D>
void InsertDataIntoInputMap(
const C& key,
const D val)
{
std::cout << "Not shared\n";
}
template<class C, class D>
void InsertDataIntoInputMap(
const C& key,
const boost::shared_ptr<D> val)
{
if (val)
{
std::cout << "Shared\n";
}
}
int main() {
int a = 0;
boost::shared_ptr<double> x(new double(4.5));
InsertDataIntoInputMap<int>(a, x);
return 0;
}
我会认为编译器在这两种情况下都应该使用带有 boost::shared_ptr 参数的函数?
【问题讨论】:
-
Coliru 无法使用 clang 和 g++ 编译第一个示例。 coliru.stacked-crooked.com/a/d1035dd01513af3a
-
-
我开始认为 Clang 和 GCC 在这里是错误的,因为第二个模板函数至少对一个参数专门化,而对另一个参数更专门化,所以无论非推断的上下文。谁能提供更好的解释?
-
@TartanLlama 对于第一个参数,两个方向的推导都失败了,所以两者都不像另一个那样专业?
-
@TartanLlama 我认为是这样,根据当前的措辞。不过,我认为CWG 1391 的 PR 会改变规则。