Is there a good / widely accepted C ++ template coding convention / standards?

I like coding standards. When writing C ++, I like coding standards. A good coding standard adds context to the language, which makes parsing difficult.

There are several commonly used standards that I think are at least a little familiar with:

  • Participant variables prefixed with 'm' or 'm _'
  • Class prefix (usually project-specific, ie in Qt all class names are prefixed with "Q")
  • Include security agreements such as "take the file name in all caps, replace." with '_' "
  • Rule of three

There are many small rules in C ++. Unfortunately, I have never been able to find rules that relate to patterns. I think the most popular template argument name is "T", but this is pointless, and if the template is not obvious, it can make the code even more difficult to read.

In any case, the main problem I am experiencing is that the templates are hard to read, and I think some conventions can be used to make them easier to read. Does anyone know of a widely used convention that makes it easy to read template code?

+3
source share
6 answers

. , ++ - Boost. , . , , , , , typedefs, .. CamelCase ( ) . , . , (, , , ForwardIteratorConcept, ForwardIterator).

, ( "m" ) - ( "" : - , - ). Java ( Java "", ++). . , , , .

+7

- . , . , ++, , ISO ++.

++ , , .

, ++ , Java. , .

, . -, , , : . -, , Qt, , .

+3

* - 'm' 'm _'

.

* ( , Qt "Q" )

. .

( RAII , ctr ).

....

. , , . , , CamelCase . , ++ , , . ForwardIter, , , .

, CamelCase (Java- - blech: p), - .

, . / /. , , . :

template < typename MyParams >
struct my_metafunction
  : mpl::if_
    <
      check // probably wouldn't actually split this one since it trivial...but as example...
      <
        MyParams
      >
    , some_type_expression
    , some_other_type_expression
    >
{};
+2

" ". , . , m m_ , Windows. .

. . , , . , , Herb Sutter Andrei Alexandrescu ++ Standards Standards. , , , . , .

, , , . , template<class Ch, class Tr>. .

+2
source

Check out Boost if you want to see their coding convention.

0
source

As others say, it depends on the coding style of the project. I like to use lowercase letters, separated by a point when encoding. And for harmony, I also use lowercase letters for the template options. To distinguish them from others, I start with an underscore and end with "_t".

`

template<typename _encoder_t>
class compression
{
typedef typename _encoder_t::settings settings_t;
...
};

`

0
source

Source: https://habr.com/ru/post/1793769/


All Articles