R. Martinho Fernandes - 4 years ago 139

C++ Question

Imagine I'm writing some container template or something. And the time comes to specialize

`std::swap`

`template <typename T>`

void swap(my_template<T>& x, my_template<T>& y) {

using std::swap;

swap(x.something_that_is_a_T, y.something_that_is_a_T);

}

This is very neat and all. Until I want to add an exception specification. My

`swap`

`noexcept`

`T`

`noexcept`

`template <typename T>`

void swap(my_template<T>& x, my_template<T>& y)

noexcept(noexcept(swap(std::declval<T>(), std::declval<T>())))

Problem is, the

`swap`

`swap`

`std::swap`

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

I think I would move it into a separate namespace

```
namespace tricks {
using std::swap;
template <typename T, typename U>
void swap(T &t, U &u) noexcept(noexcept(swap(t, u)));
}
template <typename T>
void swap(my_template<T>& x, my_template<T>& y)
noexcept(noexcept(tricks::swap(std::declval<T>(), std::declval<T>())))
{
using std::swap;
swap(x.something_that_is_a_T, y.something_that_is_a_T);
}
```

Alternatively you can move the whole code up into `tricks`

and delegate to there.

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**