Suppose you need to somehow allocate some small code to call it in many places a couple of functions. Actually I found for myself a couple of options - a template in a separate namespace or a good old macro.

Interested in what professional programmers think about these options and, perhaps, will advise something else?

// вариант №1 отдельное пространство имен, где собирается весь утилитарный хлам namespace makemove { template <class T> inline void SWAP(T & a, T & b) noexcept { T t = a; a = b; b = t; } } void board15::makemove(const directions & dir) { using namespace makemove; //#define SWAP(a,b) {int t = (a); (a) = (b); (b) = t;} // вариант № 2 switch (dir) { case UP: // там внутри не самые короткие выражения, нет смысла их приводить SWAP(board[/* ... */], board[/* ... */]); current_pos_y += 1; break; case DOWN: SWAP(board[/* ... */], board[/* ... */]); current_pos_y -= 1; break; case LEFT: SWAP(board[/* ... */], board[/* ... */]); current_pos_x += 1; break; case RIGHT: SWAP(board[/* ... */], board[/* ... */]); current_pos_x -= 1; break; } //#undef SWAP return; } 

PS in std :: swap from "utility" it is not necessary to poke, it does not concern a question.

  • I understand that using macros is considered strictly unacceptable here? - Nex
  • Not strictly unacceptable, of course. But if macros can be avoided, they should be avoided. In C ++ there are more adequate means. Ask a separate question why macros are evil, get interesting information for consideration. - VladD

3 answers 3

If you need a small function somewhere to avoid clogging the scope, you can put it in an anonymous namespace:

 namespace { template <class T> inline void SWAP(T & a, T & b) noexcept { T t = a; a = b; b = t; } } 

Thus, the function will be visible only in the file where it is defined, and will not cause linker conflicts if the same file is declared in the neighboring file. This technique makes sense when used in .cpp-files, when used in headers you, on the contrary, get many copies of the same code.

    I am not a professional programmer, since I am just unemployed, but nevertheless I will express my opinion. :)

    If this common part of the code is duplicated in several functions - members of the class board15 , then I would allocate it to a separate private function of the same class. A function can be either static or non-static, depending on whether it needs to deal with other non-static class members besides those that can be passed to it as arguments.

    For example,

     class board15 { private: static void swap( Board &board1, Board &board2 ) { // можно ее определить внутри класса, чтобы сделать ее встраиваемой // либо просто добавить спецификатор функции inline в ее объявление // если это целесообразно. } //... }; 

    That is, if this function is included in the implementation of your class and is not generalized for several classes or objects, then it should be made a member of your class.

    This does not prevent it from being also a template function.

    As for this approach

     namespace makemove { template <class T> inline void SWAP(T & a, T & b) noexcept { T t = a; a = b; b = t; } } void board15::makemove(const directions & dir) { using namespace makemove; //... 

    Then I would have rejected it immediately. It is not clear why the classes that use the function and the function itself are declared in different namespaces if this function is associated with the objects of these classes only. And instead of using directives, I would recommend at least using the declaration of this function.

      You can mark a function with the static keyword so that it is visible only in the current file. And of course, it should not be included in the header file with prototypes.