当我们定义一个模板时,总是定义“value_type”是一个好的实践吗?

时间:2022-10-06 04:15:29
template<typename T>
class Point
{
public:
    typedef T value_type;
    ...
};

I have seen above code in the book, pp176.

我在书上见过上面的代码,pp176。

Question1> Is it a good practice to always add the definition for value_type?

问题1>总是添加value_type的定义是一个好习惯吗?

Question2> Where this defined value_type will be used?

问题2>定义的value_type将在哪里使用?

For example:

例如:

Point<int>::value_type?

点< int >::value_type ?

3 个解决方案

#1


15  

It doesn't hurt to have one, but it mostly only makes sense for containers (like std::vector), as all containers provide this typedef and a uniform interface for accessing the contained values (begin/end, front/back), although this has mostly gotten obsolete in C++11 with auto and decltype. It's still cleaner to say some_template<typename container::value_type> ..., though.

有一个也无妨,但它只对容器(比如std::vector)有意义,因为所有容器都提供了这种类型定义和统一的接口来访问包含的值(开始/结束、前/后),尽管在c++ 11中,自动类型和解密类型大多已经过时了。还可以更简洁地说some_template …,虽然。 容器::value_type>

That in turn means they can be used interchangeably in generic code (the main reason why things were done that way). If it makes sense for your Point class to know what types the contained values are, well, have that typedef. As I said, it doesn't hurt. However, I have a feeling that it doesn't make too much sense for that particular example.

这反过来意味着它们可以在通用代码中互换使用(这也是为什么要这样做的主要原因)。如果让您的Point类知道包含的值是什么类型是有意义的,那么,拥有那个typedef。就像我说的,不会疼。然而,我有一种感觉,对于这个特定的例子来说,它没有太大的意义。

#2


5  

It's good practice for writing functions that perform on containers. For example, if I wrote a swap function that accepts a container (templated), and two indices to swap, then I could use the value_type definition to define a temp variable.

这是编写在容器上执行的函数的良好实践。例如,如果我编写了一个接受容器(模板化)和两个要交换的索引的交换函数,那么我可以使用value_type定义来定义一个临时变量。

 template<typename T>
 void swap(T &container, int i, int j) {
    typename T::value_type temp = container[i];
    container[i] = container[j];
    container[i] = temp;
 }

#3


1  

I'd say only if it makes sense for the type. I've run into troubles where value_type gets in the way because some generic algorithm wrongfully assumes it's a container of some sort ( in my case, I seem to recall it was some algorithm in boost that assumed shared_ptr was a container multi item container due to value_type being present ).

我想说,只有当它对类型有意义的时候。我遇到麻烦value_type妨碍因为一些通用的算法错误地假定它是某种容器(在我的例子中,我似乎记得一些算法在提高,以为要一个集装箱多项目容器由于value_type存在)。

#1


15  

It doesn't hurt to have one, but it mostly only makes sense for containers (like std::vector), as all containers provide this typedef and a uniform interface for accessing the contained values (begin/end, front/back), although this has mostly gotten obsolete in C++11 with auto and decltype. It's still cleaner to say some_template<typename container::value_type> ..., though.

有一个也无妨,但它只对容器(比如std::vector)有意义,因为所有容器都提供了这种类型定义和统一的接口来访问包含的值(开始/结束、前/后),尽管在c++ 11中,自动类型和解密类型大多已经过时了。还可以更简洁地说some_template …,虽然。 容器::value_type>

That in turn means they can be used interchangeably in generic code (the main reason why things were done that way). If it makes sense for your Point class to know what types the contained values are, well, have that typedef. As I said, it doesn't hurt. However, I have a feeling that it doesn't make too much sense for that particular example.

这反过来意味着它们可以在通用代码中互换使用(这也是为什么要这样做的主要原因)。如果让您的Point类知道包含的值是什么类型是有意义的,那么,拥有那个typedef。就像我说的,不会疼。然而,我有一种感觉,对于这个特定的例子来说,它没有太大的意义。

#2


5  

It's good practice for writing functions that perform on containers. For example, if I wrote a swap function that accepts a container (templated), and two indices to swap, then I could use the value_type definition to define a temp variable.

这是编写在容器上执行的函数的良好实践。例如,如果我编写了一个接受容器(模板化)和两个要交换的索引的交换函数,那么我可以使用value_type定义来定义一个临时变量。

 template<typename T>
 void swap(T &container, int i, int j) {
    typename T::value_type temp = container[i];
    container[i] = container[j];
    container[i] = temp;
 }

#3


1  

I'd say only if it makes sense for the type. I've run into troubles where value_type gets in the way because some generic algorithm wrongfully assumes it's a container of some sort ( in my case, I seem to recall it was some algorithm in boost that assumed shared_ptr was a container multi item container due to value_type being present ).

我想说,只有当它对类型有意义的时候。我遇到麻烦value_type妨碍因为一些通用的算法错误地假定它是某种容器(在我的例子中,我似乎记得一些算法在提高,以为要一个集装箱多项目容器由于value_type存在)。