'Creating list of unique_ptr using initialization list and make_unique fails in GCC 5.4

I am using GCC 5.4 for compiling a test program in C++ 14.

#include <type_traits>
#include <list>
#include <iostream>
#include <memory>

int main()
{
    int VALUE = 42;
    const auto list_ = {
        std::make_unique<int>(VALUE),
        std::make_unique<int>(0),
        std::make_unique<int>(0)
    };
}

GCC 5.4 fails with the below error message:

<source>: In function 'int main()':
<source>:13:5: error: use of deleted function 'std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = int; _Dp = std::default_delete<int>]'
     };
     ^
In file included from /opt/compiler-explorer/gcc-5.4.0/include/c++/5.4.0/memory:81:0,
                 from <source>:4:
/opt/compiler-explorer/gcc-5.4.0/include/c++/5.4.0/bits/unique_ptr.h:356:7: note: declared here
       unique_ptr(const unique_ptr&) = delete;
       ^

The same code compiles properly with Clang 3.5. See https://godbolt.org/z/PM776xGP4

The issue seems to be there until GCC 9.2 where it compiles properly.

Is this a known bug in GCC 9.1 and below? If yes, is there a way to solve this using the initializer list?



Solution 1:[1]

Here below is my solution and comments.

All calculation is based on the text width would not longer than it's container.

// I combind label and color together
const quad = [
  {label:'Assess',  color: '#5899DA'},
  {label:'Adopt',   color: '#8743B'},
  {label:'Avoid',   color: '#19A979'},
  {label:'__Analyze__', color: '#ED4A7B'}, // add more text
];

const margin = {
  top: 20,
  right: 65,
  bottom: 50,
  left: 65,
};

// the chart ought to be wider than taller
const width = 600 - (margin.left + margin.right);
const height = 400 - (margin.top + margin.bottom);

const svg = d3
  .select('.viz')
  .append('svg')
  .attr('viewBox', `0 0 ${width + (margin.left + margin.right)} ${height + (margin.top + margin.bottom)}`);

const group = svg
  .append('g')
  .attr('transform', `translate(${margin.left} ${margin.top})`);

const quadrantsGroup = group
  .selectAll('g')
  .data(quad)
  .enter()
  .append('g')
  .attr('class', 'quadrants')
  .attr("transform", (d, index) => {
    let x = (index == 0 || index == 2) ? 0 : width / 2,
        y = (index == 0 || index == 1) ? 0 : height / 2;
    return `translate(${x}, ${y})`;
  })
  .each(addLabel);

function addLabel (d) {
  let parent = d3.select(this); // g.quadrants
  
  // background rectangle width fill color
  parent.append("rect")
    .attr('width', width / 2).attr("height", height / 2)
    .attr("fill", d.color);
  
  // insert label rectangle and label text in sequence
  let labelRect = parent.append('rect').attr("rx", 4).attr("fill", 'white');
  let label = parent.append("text")
            .text(d.label)
            .attr("text-anchor", 'middle')
            .attr("x", width / 2 / 2);
  
  // calculate label box width native api: getBBox or getBoundingClientRect
  // https://developer.mozilla.org/en-US/docs/Web/API/SVGGraphicsElement/getBBox
  // https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect
  let labelBox = label.node().getBBox(); // or label.node().getBoundingClientRect()
  
  // assign label text and label rectangle appropriate position
  labelRect.attr("width", labelBox.width + 10)
    .attr("height", labelBox.height + 10)
    .attr("x", (width / 2 - (labelBox.width + 10)) / 2);
  
  label.attr('y', labelBox.height + 5);
}
<script src="https://d3js.org/d3.v7.min.js"></script>
<div class="viz"></div>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 1Cr18Ni9