C ++中的特征是什么,特别是在boost中

时间:2015-05-27 07:49:14

标签: c++ boost traits

我正在研究Boost Library并发现它很多地使用traits概念,比如iterator_traits,graph_traits。 特征是什么意思?你能给我一个简单而简洁的例子,告诉我们为什么我们需要特质。 据我所知,"特征"似乎意味着它包含了我们可能需要的所有类型,这样我们就不会对类型产生错误 以下是boost中的graph_traits模板:

template <typename Graph>
  struct graph_traits {
    typedef typename Graph::vertex_descriptor      vertex_descriptor;
    typedef typename Graph::edge_descriptor        edge_descriptor;
    typedef typename Graph::adjacency_iterator     adjacency_iterator;
    typedef typename Graph::out_edge_iterator      out_edge_iterator;
    typedef typename Graph::in_edge_iterator       in_edge_iterator;
    typedef typename Graph::vertex_iterator        vertex_iterator;
    typedef typename Graph::edge_iterator          edge_iterator;

    typedef typename Graph::directed_category      directed_category;
    typedef typename Graph::edge_parallel_category edge_parallel_category;
    typedef typename Graph::traversal_category     traversal_category;

    typedef typename Graph::vertices_size_type     vertices_size_type;
    typedef typename Graph::edges_size_type        edges_size_type;
    typedef typename Graph::degree_size_type       degree_size_type;
  };

2 个答案:

答案 0 :(得分:2)

我将解释如何通过一个简单的样本看到一个traits类。

定义可能是:特征允许以非侵入方式扩展T。

示例

想象一下,您想要提供一个包含2D点概念(X,Y坐标)的几何库。 你提供了

/*
 * @tparam T the coordinate type
 */
template <typename T>
class Point2D
{
    T _x;
    T _Y;
public:
    /* some ctors */

    /*
     * Multiply this point by the given factor. 
     * @post this._x = factor * this._x
     *       this._y = factor * this._y
     * @return reference to *this.
     */
    Point2D<T> operator*=(double factor);
};

您选择模拟Point2D类,以便您的lib的用户可以选择适当的类型(如果需要精度则为double,如果他使用像素,则为int,...)。例如,在Qt中,它们将int强制为坐标类型,它可以阻止您的项目。 类型T需要提供有关坐标类型概念的一些信息:在Point2D类中,您需要使用T:

  • 获取标量类型T是可乘的(在我的算子中* =,我写了双倍但它可能过于干扰)
  • 获取T
  • 的字符串表示形式
  • 比较2 T(实现运营商==)......

如果您编写自己的Coordinate类,则可以提供所有内容。但是如果你的库的用户想要使用int作为T,他就不能扩展int。

这里有特征:你的Point2D将使用traits类CoordTypeTraits。它的目的是“扩展”T类型以提供您需要的所有东西,从T作为坐标概念(功能,typedef ...)

样品:

typename <typedef T>
struct CoordTypeTraits
{
    /*
     * Define the scalar type T is multipiable with
     */ 
    // typedef ... ScalarType;

    /*
     * Check the equality between 2 T, with a given precision
     */
    // static bool IsCloseTo(const T& coord1, const T& coord2);

    /*
     * Return a string representation of T.
     */
     // static string ToString(const T& coord);
}

现在,您可以在代码中访问有关T的信息,这要归功于特征类CoordTypeTraits:

/*
 * @tparam T the coordinate type
 */
template <typename T>
class Point2D
{
    T _x;
    T _Y;
public:
    /* some ctors */

    /*
     * @post this._x = factor * this._x
     *       this._y = factor * this._y
     * @return reference to *this.
     */
    Point2D<T> operator*=(CoordTypeTraits<T> factor);

    const bool operator==(const T& rhs)
    {
        return CoordTypeTraits<T>.IsCloseTo(*this, rhs);
    }

    string ToString() const
    {
        return CoordTypeTraits<T>.ToString();
    }
};

你的lib的用户将使用你的Point2D类型,并且他必须提供(通过专门针对其类型的CoordTypeTraits)一个特征来将坐标概念的数据“添加”到T。

例如,使用double:

typedef Point2D<double> Point_double;

// specialization of CoordTypeTraits for double coordinate type
template<>
struct CoordTypeTraits<double>
{
    typedef double ScalarType;

    static bool IsCloseTo(const T& coord1, const T& coord2)
    {
        return (coord1 - coord2 < GetPrecision()) && (coord2 - coord1 < GetPrecision());
    }

    private:
    static const double GetPrecision()
    {
        return 0.001; // because double is micrometers by convention, and I need nanometer precision
    }

    static string ToString(const double& coord)
    {
        return boost::lexical_cast<string>(coord);
    } 
}

例如使用int:

typedef Point2D<int> Point_int;

// specialization of CoordTypeTraits for double coordinate type
template<>
struct CoordTypeTraits<int>
{
    typedef int ScalarType;

    static bool IsCloseTo(const T& coord1, const T& coord2)
    {
        return coord1 == coord2;
    }

    static string ToString(const int& coord)
    {
        return boost::lexical_cast<string>(coord);
    } 
}

结论和备注

您的lib提供了Point2D类,因此用户可以使用您提供的所有功能(比较,翻译,旋转......)。 如果他提供一个特征来处理它的类型作为坐标,他可以使用他想要的任何坐标类型。 通常,库提供了一些共同的特征(这里我们将提供Point_double和Point_int)。

备注:我没有尝试编译,代码只是为了说明。

答案 1 :(得分:0)

可以使用&#34;特征&#34;替换(作为一个单词)特征。

它们是一种C ++技术,它使用模板特化将不同类型,操作和常量关联到一个类型(有效地创建元数据)。