std::pair copy and move constructor

Daniel Krügler daniel.kruegler@gmail.com
Wed Mar 6 21:07:00 GMT 2013


2013/3/6 François Dumont <frs.dumont@gmail.com>:
> On 03/05/2013 11:03 PM, Daniel Krügler wrote:
>>
>> 2013/3/5 Jonathan Wakely <jwakely.gcc@gmail.com>:
>>>
>>> On 5 March 2013 20:36, François Dumont wrote:
>>>>
>>>>      I finally wonder if the compiler couldn't help here. If the copy
>>>> assignment is deleted why wouldn't the compiler try to use the template
>>>> assignment operator ?
>>>
>>> If an operation is deleted that means the class author (implicitly or
>>> explicitly) said "this operation is not allowed"
>>>
>>> If the compiler finds a way to work around the author's intention by
>>> choosing a different function then what's the point in having deleted
>>> functions?
>>
>> My understanding of Francois' suggestion would allow to declare the
>> special members as defaulted, and to provide the template versions
>> only as the fall-back. In this case the effect would be that
>> std::pair<int, double> would use the defaulted copy assignment
>> operator (cao), so the default is fine. In case of std::pair<int&,
>> double>, the defaulted coa would be deleted and the compiler would
>> still find the (constrained) template assignment operator (tao),
>> having the effect of the current user-defined one. In case of
>> std::pair<const int, double> the cao is again deleted, the compiler
>> tries the tao (which can be designed to reject this cases as well),
>> and we have a fully constrained type. This sounds reasonable to me and
>> it emulates the effect of a built-in rule for a valid copy-assignment
>> operator for references to the best possible way. I also understand
>> him to restrict this rule to the special member functions. This
>> approach looks like an extension of CWG 1402 to me (which introduces a
>> similar rule for move special members). I wouldn't like to have this
>> rule for other functions and it would still not be possible to
>> constrain a destructor, but that is life ;-)
>>
>> - Daniel
>>
>     That's indeed what I had in mind. And yes, limited to special methods, I
> can't even imagine how it could be generalized to others.
>
>     Note that in the case of std::pair<cont int, double> the template
> assignment operator would fail to compile too so it would be transparent.
> The only difference would be that std::is_copy_assignable<std::pair<const
> int, double>> would give the correct result. Add
> std::is_copy_assignable<std::pair<int&, double>> too which is of course
> std::false_type. So it would be very nice.

I agree that this *could* be made to work via a such a core language
extension. My personal hope is a slighter rule change of the standard
library that would have the effect to allow that std::pair also can
have a base class. With such an allowance it is possible to realize
the same thing without any language change.

- Daniel



More information about the Libstdc++ mailing list