Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eclipse indexer can't resolve shared_ptr

Tags:

c++

c++11

eclipse

After researching this on the internet, I've been unable to get the Eclipse indexer to resolve "shared_ptr" from the C++0x additions that come with GCC 4.4.4. I made sure to create my project with the proper includes for Eclipse, so it's definitely looking in the the 4.4.4 include folders.

The program compiles and runs just fine. To access shared_ptr I'm using "#include <memory>".

Any idea what's breaking the indexer?

like image 790
Dylan Klomparens Avatar asked Nov 29 '11 14:11

Dylan Klomparens


People also ask

What happens when shared_ptr goes out of scope?

All the instances point to the same object, and share access to one "control block" that increments and decrements the reference count whenever a new shared_ptr is added, goes out of scope, or is reset. When the reference count reaches zero, the control block deletes the memory resource and itself.

What is the purpose of the shared_ptr <> template?

std::shared_ptr. std::shared_ptr is a smart pointer that retains shared ownership of an object through a pointer. Several shared_ptr objects may own the same object.

What is shared_ptr?

shared_ptr is a smart pointer which can share the ownership of object (managed object). 1. Several shared_ptr can point to the same object (managed object). 2. It keep a reference count to maintain how many shared_ptr are pointing to the same object.

Can you return a shared_ptr?

So the best way to return a shared_ptr is to simply return by value: shared_ptr<T> Foo() { return shared_ptr<T>(/* acquire something */); }; This is a dead-obvious RVO opportunity for modern C++ compilers. I know for a fact that Visual C++ compilers implement RVO even when all optimizations are turned off.


2 Answers

You need to set the pre-processor symbol '__GXX_EXPERIMENTAL_CXX0X__' to the eclipse project. g++ automatically adds that when you use '-std=c++0x', but eclipse is not aware of that, so it treats those sections of the relevant headers as disabled.

like image 168
Dave S Avatar answered Sep 20 '22 01:09

Dave S


I experienced this problem under Windows with Eclipse 4.5.1 (Mars.1) and Cygwin 2.3.0 (GCC 4.9.3).

The indexer can't find shared_ptr because of lines like this in the <memory> header. The __cplusplus macro is evaluating to something other than C++ 11 (aka 201103) so the older auto_ptr.h is being included instead of shared_ptr.h. Why? The below screen shot of the project properties shows that C++ 98 (199711) is being detected under CDT GCC Build-in Compiler Settings.

#if __cplusplus >= 201103L #  include <bits/shared_ptr.h> #else #  include <backward/auto_ptr.h> #endif 

enter image description here

There are two possible solutions to tell Eclipse to use C++ :

  • On the same Preprocessor Include Paths screen, scroll to the top of the Setting Entries area. Expand CDT User Setting Entries. Add a new Preprocessor Macro for __cplusplus=201103L. Do this for both the Release and Debug Configurations. Then rebuild the index.

  • If you want to default the CDT GCC Build-in Compiler Settings to use 201103 for all projects, then edit the language.settings.xml file (under Windows this is c:\Users\deanhill\workspace\.metadata\.plugins\org.eclipse.cdt.core\language.settings.xml). Set __cplusplus=201103L. Restart Eclipse and rebuild the index.

like image 35
Dean Hill Avatar answered Sep 21 '22 01:09

Dean Hill