为什么traits :: access无法编译(c ++)?

时间:2016-07-04 15:47:22

标签: c++ boost typetraits boost-geometry

我现在正在学习如何使用boost::geometry库,我正在遵循教程,其中引入了类型特征以使代码更通用。例如,下面的distance函数通过使用特征被视为遗传:

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  

有什么想法吗?谢谢。

1 个答案:

答案 0 :(得分:2)

您有专门的traits::access而未提供通用定义,因此请添加:

namespace traits
{
    template <typename, int>
        struct access;
    /* ... */
}