Template Parameters in C ++

Suppose that I have an arbitrary template method that could receive parameters by value and by reference const (obviously, for trivial types and for objects, respectively).

How is this situation handled when writing prototype templates?

I could do something like:

template <typename T> void Foo(T value) {
   // Do something.
}

template <typename T> void Foo(const T& value) {
   // Do something, yeah.
}

// Specialization for first prototype. 
template <> void Foo<int>(int value) { }

// Specialization for second prototype. 
template <> void Foo<Object>(const Object& value) { }

But this approach is only suitable for trivial functions that act as a wrapper for some other calls.

If a function (not a templated version) contains a lot of code inside it, it means that I would have to copy the code twice.

Can I do something smarter here?

+3
source share
2

reference , const.

+7

const , .

+5

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


All Articles