表示模板类型的名称是单个字符吗? [英] Should names representing template types be a single character?

查看:141
本文介绍了表示模板类型的名称是单个字符吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在根据这些指南定义一些C ++编码风格指南。数字8说表示模板类型的名称应该是单个大写字母。说明:在C ++开发社区中的常见做法,这使得模板名称相对于所使用的所有其他名称而言显得突出。

we're defining some C++ coding style guidelines based on these guidelines. Number 8 says "Names representing template types should be a single uppercase letter." Explanation: "Common practice in the C++ development community. This makes template names stand out relative to all other names used."

这真的很常见吗?我同意让 template< class T> class vector {...} 。但是如果我有几个模板参数呢?我不认为< class A,class B> < class AllocationPolicy,ThreadingPolicy类< / code>。

Is it really that common? I agree that it makes a lot of sense to have template<class T> class vector {...}. But what if I have several template Arguments? I don't think that <class A, class B> is easier to understand than <class AllocationPolicy, class ThreadingPolicy>.

您是否同意经常有不应用给定规则的情况(根据1 ...允许)

Do you agree that there are often cases when the given rule should not be applied (which is allowed according to 1...)?

感谢您的想法!

推荐答案

我不同意命名约定。对我来说,表示完全泛型类型的模板参数作为一个简单的字母有意义 - 这是一个类型 T ,我真的不在乎哪个。但是如果对类型有任何要求,产生一个标识模板参数是什么的名称是有意义的: template< typename T,typename Allocator> struct container - 第一个类型是泛型,任何 T 将适合,但第二个是分配器。

I don't really agree with that naming convention. For me, template parameters representing a fully generic type make sense as a simple letter --this is a type T, I don't really care which. But if there is any requirements on the type, it makes sense to produce a name that identifies what the template argument is: template <typename T, typename Allocator> struct container -- the first type is generic, any T will fit, but the second one is an allocator.

这是没有什么不同的函数,你不希望你的参数被调用 a b ...按顺序或外观,而是产生有意义的名称作为文档的一部分。此外,如果开发样式指南,我将考虑在模板类的情况下要求模板参数的局部typedef,以及对类型的需求的静态断言(只要可能)。

It is no different to functions, where you do not want your parameters to be called a, b... in order or appearance, but rather produce meaningful names as part of the documentation. Also, if developing a style guide, I would consider requiring local typedefs of the template arguments in the case of template classes together with static asserts on the requirements for the types (whenever possible). That is, at least until concepts provide that functionality.

我认为STL是库的一个很好的例子,你可以看到,生成typedef帮助读取代码(从g ++ 4.2 stl):

I consider the STL to be a good example of library and you can see that generating typedefs help in reading the code (from g++ 4.2 stl):

类:

// from <vector> -> <bits/stl_vector.h>
template<typename _Tp, typename _Alloc = std::allocator<_Tp> >
class vector : protected _Vector_base<_Tp, _Alloc>
{
  // Concept requirements.
  typedef typename _Alloc::value_type _Alloc_value_type; // _Alloc contains value_type internal type
  __glibcxx_class_requires(_Tp, _SGIAssignableConcept)   // _Tp is assignable
[...]
public:
   typedef _Tp value_type;                 // from here on _Tp is not mentioned
   typedef typename _Tp_alloc_type::reference reference; // check + typedef
[...]
   iterator begin() { 
   // without typedef:
   // __gnu_cxx::__normal_iterator<pointer, vector_type> begin() {
[...]
   reference operator[](size_type __n)
   // _Tp& operator[](size_type __n)
[...]

// from <memory>
template <typename _Tp>
class auto_ptr 
{
[...]
    template <typename _Tp1>
    auto_ptr(auto_ptr<_Tp1>& __a) throw() 

/ p>

And functions:

template <typename _InputIterator, typename _Tp>
inline _InputIterator find( _InputIterator __first, _InputIterator __last, _Tp value ) {
  // concept requirements
  __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
  [...]

所有他们的名称前缀为 _ 字母或第二个 _ ,它们是保留用于实现的名称,他们更喜欢 _Tp over _T ,但是最后你可以看到每当一个类型是泛型时,它被称为 _Tp code> _Tp1 ...,而当它有一些特殊的要求,选择一个更明智的名字。

All of their names are prefixed by _ and either a capital letter or a second _, which are the names reserved for the implementation, and they prefer _Tp over _T, but at the end you can see that whenever a type is generic it is called _Tp, _Tp1..., while when it has some specific requirements attached, a more sensible name is chosen.

它们之间的细线,例如在 std :: vector 中,对泛型类型有实际的要求: _Tp 必须是可分配的,但在一天结束时,它是大多数泛型类型。调用 _Assignable 在有多个需求的容器中会很奇怪。

There is a fine line in between them, as for example in std::vector, there are actual requirements on the generic type: _Tp must be assignable, but at the end of the day, it is a mostly generic type. Calling it _Assignable would be weird in some containers where there are more than one requirement.

这篇关于表示模板类型的名称是单个字符吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆