I have a subclass of std::unique_ptr
and am trying to use it with std::variant
. I have the following setup
// main.cc
#include <iostream>
#include <variant>
using namespace std;
class Content {
public:
Content() = default;
~Content() { cout << "deconstructing Content" << endl; };
int value = 10;
};
template<typename T>
class Wrapper : public unique_ptr<T> {
public:
Wrapper(T *value): unique_ptr<T>(value) {};
~Wrapper() { cout << "deconstructing Wrapper" << endl; };
};
static variant<Wrapper<Content>, int> do_sth(bool flag) {
if (flag) return Wrapper(new Content());
return 1;
}
int main(int argc, const char *argv[]) {
auto result = do_sth(true);
if (auto wrapper = get_if<Wrapper<Content>>(&result)) {
cout << wrapper->get()->value << endl;
} else {
cout << *get_if<int>(&result) << endl;
}
return 0;
}
compiled on macOS 10.14 with Xcode 10.1 using
$ #c++ --version -> Apple LLVM version 10.0.0 (clang-1000.11.45.5)
$ c++ -std=gnu++17 main.cc
The compiler complaints with the following
main.cc:25:12: error: no viable conversion from returned value of type 'Wrapper<Content>' to function return type 'variant<Wrapper<Content>, int>'
return Wrapper(new Content());
^~~~~~~~~~~~~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/variant:1142:3: note: candidate constructor not viable: no
known conversion from 'Wrapper<Content>' to 'const std::__1::variant<Wrapper<Content>, int> &' for 1st argument
variant(const variant&) = default;
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/variant:1155:13: note: candidate template ignored:
substitution failure [with _Arg = Wrapper<Content>, $1 = 0, $2 = 0, $3 = 0]: no type named 'type' in
'std::__1::result_of<std::__1::__variant_detail::__overload<Wrapper<Content>, int> (Wrapper<Content> &&)>'
constexpr variant(_Arg&& __arg) noexcept(
^
1 error generated.
I have two questions: First, what am I doing wrong? Second, when I remove the deconstructor of Wrapper
, i.e.,
template<typename T>
class Wrapper : public unique_ptr<T> {
public:
Wrapper(T *value): unique_ptr<T>(value) {};
};
then it compiles and runs with the following output
10
deconstructing Content
Why does it (seem to?) work without deconstructor?
Aucun commentaire:
Enregistrer un commentaire