Bug 16033 – std.algorithm.sorting.sort should be possible with const/immutable object

Status
RESOLVED
Resolution
INVALID
Severity
enhancement
Priority
P1
Component
phobos
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2016-05-17T11:54:00Z
Last change time
2016-05-17T13:11:09Z
Assigned to
nobody
Creator
greensunny12

Comments

Comment #0 by greensunny12 — 2016-05-17T11:54:58Z
The following yields: void main() { import std.algorithm.sorting : sort; const(int)[] arr = [1, 2, 3]; arr.sort(); } foo.d(6): Error: template std.algorithm.sorting.sort cannot deduce function from argument types !()(const(int)[]), candidates are: /usr/include/dlang/dmd/std/algorithm/sorting.d(1027): std.algorithm.sorting.sort(alias less = "a < b", SwapStrategy ss = SwapStrategy.unstable, Range)(Range r) if ((ss == SwapStrategy.unstable && (hasSwappableElements!Range || hasAssignableElements!Range) || ss != SwapStrategy.unstable && hasAssignableElements!Range) && isRandomAccessRange!Range && hasSlicing!Range && hasLength!Range) In my understanding elements can be immutable and const, but assigning elements isn't. So shouldn't it be possible to use emplace to swap them around?
Comment #1 by ag0aep6g — 2016-05-17T13:09:27Z
> In my understanding elements can be immutable and const, but assigning > elements isn't. So shouldn't it be possible to use emplace to swap them > around? Swapping stuff around means writing (to both locations). But you can't overwrite immutable data, and you can't write through a const reference. So no, you can't swap const or immutable elements. Doing so would break the guarantees that const and immutable give. Closing as invalid. Please reopen if I'm missing something. Or maybe make a thread on D.learn if the details of immutable and const are not clear. http://forum.dlang.org/group/learn