[cfe-dev] swap, noexcept and non-movable types
Howard Hinnant
hhinnant at apple.com
Tue May 31 10:53:50 PDT 2011
Consider this C++0x code:
template <class T>
T&&
declval() noexcept;
template <class T>
struct some_trait
{
static const bool value = false;
};
template <class T>
void swap(T& x, T& y) noexcept(some_trait<T>::value)
{
T tmp(static_cast<T&&>(x));
x = static_cast<T&&>(y);
y = static_cast<T&&>(tmp);
}
template <class T, unsigned N>
struct array
{
T data[N];
void swap(array& a) noexcept(noexcept(swap(declval<T&>(), declval<T&>())));
};
struct DefaultOnly
{
DefaultOnly() = default;
DefaultOnly(const DefaultOnly&) = delete;
DefaultOnly& operator=(const DefaultOnly&) = delete;
~DefaultOnly() = default;
};
int main()
{
array<DefaultOnly, 1> a, b;
}
We currently do not compile this code. The errors are:
test.cpp:14:7: error: call to deleted constructor of 'DefaultOnly'
T tmp(static_cast<T&&>(x));
^ ~~~~~~~~~~~~~~~~~~~
test.cpp:24:43: note: in instantiation of function template specialization 'swap<DefaultOnly>' requested here
void swap(array& a) noexcept(noexcept(swap(declval<T&>(), declval<T&>())));
^
test.cpp:30:5: note: function has been explicitly marked deleted here
DefaultOnly(const DefaultOnly&) = delete;
^
test.cpp:15:7: error: overload resolution selected deleted operator '='
x = static_cast<T&&>(y);
~ ^ ~~~~~~~~~~~~~~~~~~~
test.cpp:31:18: note: candidate function has been explicitly deleted
DefaultOnly& operator=(const DefaultOnly&) = delete;
^
2 errors generated.
I recently learned that a recent gcc 4.7 snapshot /does/ compile this code.
So I'm asking the language lawyers here: should we compile this code or not?
The underlying question is whether or not swap<DefaultOnly>() should be instantiated. It is not called except as an unevaluated operand.
Thanks,
Howard
More information about the cfe-dev
mailing list