【发布时间】:2016-07-04 15:47:21
【问题描述】:
我现在正在学习如何使用boost::geometry 库,并且我正在关注教程,其中引入了类型特征以使代码更通用。例如,下面的distance函数通过使用traits被认为是遗传的:
struct mypoint
{
double x, y;
};
template <typename P1, typename P2>
double distance(P1 const& a, P2 const& b)
{
double dx = get<0>(a) - get<0>(b);
double dy = get<1>(a) - get<1>(b);
return std::sqrt(dx * dx + dy * dy);
}
template <typename P1, typename P2>
double distance(P1 const& a, P2 const& b)
{
double dx = get<0>(a) - get<0>(b);
double dy = get<1>(a) - get<1>(b);
return std::sqrt(dx * dx + dy * dy);
}
namespace traits
{
template <>
struct access<mypoint, 0>
{
static double get(mypoint const& p)
{
return p.x;
}
};
template <>
struct access<mypoint, 1>
{
static double get(mypoint const& p)
{
return p.y;
}
};
}
但是,当我编译代码时,会发生以下编译错误:
Error 3 error C2913: explicit specialization; 'traits::access' is not a specialization of a class template
有什么想法吗?谢谢。
【问题讨论】:
-
这个词是“通用的”,而不是“遗传的”:)
标签: c++ boost typetraits boost-geometry