I am using std::unique_ptr in this piece of code which compiles and runs as I expected.
std::stringstream out;
out << std::setw(3) << std::setfill('0') << i;
std::unique_ptr<std::string> s(new std::string(out.str()));
s->insert(s->end()-2, 1, '.');
return std::move(s);
However, I am getting error messages from Eclipse CDT. At the fourth line: Method 'insert' could not be resolved, Method 'end' could not be resolved.
Previously, I was also getting errors on appearances of the name std::unique_ptr. This was solved by setting the pre-processor symbol __GXX_EXPERIMENTAL_CXX0X__
and rebuilding the index, as described in the answer to this question.
Is there a way to make CDT understand that s is of type std::string * and that it should look in std::string for s->insert() and s->end() ?
PS: I am using Eclipse 3.7.1 and CDT 8.0.0.201106081058
PS2: I would have liked to post this as a comment in the above question, but I can't, presumably because I am a new user
It seems as if the Eclipse CDT indexer is not able to deduce the unique_ptr::pointer type that is also used as the return type of operator->(). You can see this when you type something like
std::unique_ptr<Type *> ptr;
ptr.reset(new Type);
an error will be "detected" that there would be no matching overload, and that the only candidate would be reset(?)
. So this is obviously a bug.
This issue has been recently fixed, in cdt 8.1.1. Just go help->check for updates and it will be downloaded and installed. I've tested unique_ptr and it is properly indexed.
I have the same issue on newer version of Eclipse CDT (9.3). I tried all the tricks I found on the internet, rebuilding every time my index, hoping for a change. But the indexer was never able to deduce the type of std::unique_ptr<T>::operator->()
. Finally, I decided to use a very simple workaround:
# ifdef ECLIPSE_INDEXER_WORKAROUND
MyType* my_var;
# else
std::unique_ptr<MyType> my_var;
# endif
I add ECLIPSE_INDEXER_WORKAROUND
to preprocessor symbols (of course for indexing options only, not building) in Eclipse, and indexing is useful again!
In order to pollute the code less, we can use a macro:
# ifdef ECLIPSE_INDEXER_WORKAROUND
# define MY_UNIQUE_PTR( type ) type*
# else
# define MY_UNIQUE_PTR( type ) std::unique_ptr< type >
# endif
MY_UNIQUE_PTR( int ) pint{ new int(42) };