I'm making a visitor that constructs the object model for my application after parsing something from file.
This object model includes unique_ptrs to hold ownership for some objects. When returning these from visitor methods, I get compilation errors having to do with unique_ptrs being put in the Any class.
(FYI: I only recently learned about unique_ptr and move semantics).
Here's a minimal example that shows the error:
#include <memory>
#include "antlr4-runtime.h"
int main()
{
std::unique_ptr<int *> a;
antlrcpp::Any b(std::move(a));
return 0;
}
The error I get with clang-3.9.1 looks like this (but gcc's is similar):
In file included from testje.cpp:2:
In file included from antlr4-cpp-runtime-prefix/include/antlr4-runtime/antlr4-runtime.h:30:
In file included from antlr4-cpp-runtime-prefix/include/antlr4-runtime/InterpreterRuleContext.h:8:
In file included from antlr4-cpp-runtime-prefix/include/antlr4-runtime/ParserRuleContext.h:8:
In file included from antlr4-cpp-runtime-prefix/include/antlr4-runtime/RuleContext.h:8:
In file included from antlr4-cpp-runtime-prefix/include/antlr4-runtime/tree/ParseTree.h:8:
antlr4-cpp-runtime-prefix/include/antlr4-runtime/support/Any.h:112:48: error: call to deleted constructor of 'std::unique_ptr<int *, std::default_delete<int *> >'
template<typename U> Derived(U&& value_) : value(std::forward<U>(value_)) {
^ ~~~~~~~~~~~~~~~~~~~~~~~
antlr4-cpp-runtime-prefix/include/antlr4-runtime/support/Any.h:118:18: note: in instantiation of function template specialization 'antlrcpp::Any::Derived<std::unique_ptr<int *, std::default_delete<int *> > >::Derived<const std::unique_ptr<int *, std::default_delete<int *> > &>' requested here
return new Derived<T>(value);
^
antlr4-cpp-runtime-prefix/include/antlr4-runtime/support/Any.h:112:26: note: in instantiation of member function 'antlrcpp::Any::Derived<std::unique_ptr<int *, std::default_delete<int *> > >::clone' requested here
template<typename U> Derived(U&& value_) : value(std::forward<U>(value_)) {
^
antlr4-cpp-runtime-prefix/include/antlr4-runtime/support/Any.h:44:29: note: in instantiation of function template specialization 'antlrcpp::Any::Derived<std::unique_ptr<int *, std::default_delete<int *> > >::Derived<std::unique_ptr<int *, std::default_delete<int *> > >' requested here
Any(U&& value) : _ptr(new Derived<StorageType<U>>(std::forward<U>(value))) {
^
testje.cpp:7:17: note: in instantiation of function template specialization 'antlrcpp::Any::Any<std::unique_ptr<int *, std::default_delete<int *> > >' requested here
antlrcpp::Any b(std::move(a));
^
/usr/lib/gcc/x86_64-pc-linux-gnu/5.4.0/include/g++-v5/bits/unique_ptr.h:359:7: note: 'unique_ptr' has been explicitly marked deleted here
unique_ptr(const unique_ptr&) = delete;
^
1 error generated.
I'm not sure where the clone method is actually used, but from looking at the name I would say it makes sense that it would do a copy.
Any idea how to fix this? I'm using Antlr 4.7.
Regards,
Maurice.