[cfe-dev] swap, noexcept and non-movable types
Sean Hunt
scshunt at csclub.uwaterloo.ca
Tue May 31 13:00:29 PDT 2011
On 05/31/11 11:26, Sean Hunt wrote:
> On 05/31/11 10:53, Howard Hinnant wrote:
>> 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
>
> We definitely shouldn't be instantiating swap here. Looks like a bug.
> I'll poke at it a bit.
>
> Sean
Fixed in r132350.
Sean
More information about the cfe-dev
mailing list