为什么 traits::access 无法编译(c++)?
Why traits::access cannot be compiled (c++)?
我现在正在学习如何使用 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
有什么想法吗?谢谢。
您已经专门化 traits::access
而没有给出通用定义,因此添加:
namespace traits
{
template <typename, int>
struct access;
/* ... */
}
我现在正在学习如何使用 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
有什么想法吗?谢谢。
您已经专门化 traits::access
而没有给出通用定义,因此添加:
namespace traits
{
template <typename, int>
struct access;
/* ... */
}