Should I perform my functions as general as possible?

template<class T>
void swap(T &a, T &b)
{
    T t;

    t = a; 
    a = b;
    b = t;
}

replace

void swap(int &a, int &b)
{
    int t;

    t = a;
    a = b;
    b = t;
}

This is the simplest example I could come up with, but there should be many other complex functions. Should I use all the methods that I can write, if possible?

Any flaws to do this?

thank.

+3
source share
8 answers

The advantage is that it can be reused. However, write general things only if:

  • It doesn’t take much time than to do it is not common
  • This doesn't complicate the code anymore than a general solution.
  • You know that it will benefit later.

However, know the standard library . The case you presented is already in STL as std::swap.

, , , . , , , , .

, . . , .

` " - ". -_-.

+11

, . . , , . .

+10

- , , :

, .

+4

, . (, ),

+3

, ,

, - , .

, . , , , .

+1

. () .

, , .

0

. , . , .

0

:

0

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


All Articles