0

The problem is really simple. I have a code:

#include <string>
#include <memory>
#include <vector>
#include <boost/variant.hpp>
#include <QVector>

int main() {
    QVector<boost::variant<std::unique_ptr<std::string>, int>> qt_vector;
    std::string* qt_test = new std::string;
    qt_vector.push_back(std::unique_ptr<std::string>(qt_test));

    std::vector<boost::variant<std::unique_ptr<std::string>, int>> std_vector;
    std::string* std_test = new std::string;
    std_vector.push_back(std::unique_ptr<std::string>(std_test));
}

And first version (qt) gives me a compilation error, while std's version works just fine. I have looked on std::vector source code and I think I understood (more or less) what is going there, but I could not find the source code for QVector (I have looked in Qt repository).

The question is: what is the difference between std::vector::push_back and QVector::push_back and what can I do in this situation to use QVector instead of std::vector.

P.S. I want to have a vector of boost::variant<std::unique_ptr<std::string>, int> and this example very closely resembles what I'm trying to do in my main code. I'm trying to use QVector because in all other parts of my app I have also used Qt containers

P.S. Error message is:

use of deleted function ‘std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = std::__cxx11::basic_string<char>; _Dp = std::default_delete<std::__cxx11::basic_string<char> >]’

P.S This question is different from this one because it asks about specific example and provides the context of an issue caused by the problem that was discussed in mentioned question. I also provide a concrete example that could be useful for a person who is trying to understand a problem.

haxscramper
  • 775
  • 7
  • 17
  • 2
    In short: [Qt containers require copyable elements](https://stackoverflow.com/q/32584665/1782465). – Angew is no longer proud of SO Jun 25 '18 at 18:40
  • @Angew Thanks, that's resolved my issue. If you post this as an answer I will accept it. – haxscramper Jun 25 '18 at 18:46
  • 1
    Unrelated, but you really should prefer std containers over the Qt ones where you can. The Qt containers are mostly there for legacy compatibility reasons for old Qt code and they don't receive much love. Also, Qt people don't exactly advocate their use (https://marcmutz.wordpress.com/effective-qt/containers/) much. – Jesper Juhl Jun 25 '18 at 18:50
  • "I want to have a vector of boost::variant<" - why not `std::variant`? The boost headers are fairly heavy/expensive compile-time wise, the std headers are usually lighter. Plus, boost is an extra dependency. – Jesper Juhl Jun 25 '18 at 18:54
  • @JesperJuhl I'm using c++14 which means don't have it by default (I'm using this version because Qt does not support c++17 (to my understanding)). I use boost for other purposes, not only for the variant. Would you suggest using some lightweight library for this purpose (such as [this](https://github.com/mpark/variant) or stick with boost::variant? – haxscramper Jun 25 '18 at 19:02
  • @Kamanji I'm using Qt 5.11.1 with C++17 without problems. Anyway, boost variant is fine - not saying otherwise. Just saying; use `std::variant` *if you can* :-) – Jesper Juhl Jun 25 '18 at 19:07
  • @JesperJuhl I'm sorry for bothering you again, but could you please tell me how you managed to get C++17 working for you. I've just downloaded Qt 5.11.1, created a new project, selected GCC_5.11.1 and tried every possible combination of CONFIG/QMAKE_CXXFLAGS c++17/c++1z but nothing has worked for me. I'm running Ubuntu 16_04 – haxscramper Jun 26 '18 at 12:49
  • @Kamanji No bother. This is what I use and what I did in a nutshell: I'm using CentOS7 and first installed devtoolset-7 which gives me gcc 7.3.1 rather than the rather old gcc 4.8 system compiler. Then I cloned the Qt git repository (checked out 5.11.1) and created a custom rpm.spec file for Qt (loosely based on the one from Fedora 28) that would build all of Qt into a single RPM that installs to /opt/myproject/qt (with appropriate rpath and prefix options). I then built the RPM using gcc 7.3.1 and installed it. Then pointed my project at my custom Qt and set `-std=c++17`. Works quite nicely. – Jesper Juhl Jun 27 '18 at 18:26

0 Answers0