简体   繁体   中英

C++ namespace “hiding” appearing in the Eclipse parser

Recently I have being working on a project using C++ as the programming language and Eclipse CDT as the programming IDE. The 'Chrono' library is used in the project.

I was trying to define the "<<" stream operator for different time scales like nanoseconds by putting the definitions in the same namespace as chrono, namely "std::chrono". One small example of the code of the header file (Test.hpp) is illustrated as following:

#include <chrono>
#include <iostream>

namespace test{ namespace chrono{
  typedef std::chrono::nanoseconds  nanoseconds;
}}

namespace std{ namespace chrono{
  inline std::ostream& operator<<(std::ostream& s, nanoseconds dur)
  {
     return s << dur.count() << "ns";
  }
}} 

The above code together with other parts of the project can be compiled correctly. However, the IDE, Eclipse CDT, keeps complaining "Type 'std::chrono::nanoseconds' could not be resolved" and the auto-completion functionality says "No Default Proposals" for any member variables/functions in the namespace "std::chrono". It looks like that adding new functions into the "std::chrono" namespace in this header file somehow 'hides' other content from the Eclipse's point of views.

The question is what could be the reason leading to such 'error' messages in Eclipse CDT or it is one flaw in my programming? I would appreciate any help or hint from you.

I also copy-past the code into Xcode on the laptop and there is no such error message as in Eclipse CDT.

Additional information :

The os I am using is Mac OS, thus the chrono library is slightly different from that mentioned in the answer. The screenshot of 'chrono.hpp' is as following:

在此处输入图片说明

Actually, my CDT has no issue to find the members in the namespace 'std::chrono::'. What confuses me is CDT's behaviour when I add/override members in the namespace 'std::chrono::'. See the following pictures:

Errors appear when I override a member function in the namespace: 当我覆盖命名空间中的成员函数时出现错误

Errors do not appear when I do nothing on the namespace: 当我对命名空间不执行任何操作时,不会出现错误

Any idea on how to solve this problem?

Assumptions about your setup

I believe you have changed your build settings to use -std=c++0x or something similar as the chrono library requires it.

Perhaps you did it like this: C ++设置

At the top of chrono (header file) there is a bit like this:

#if __cplusplus < 201103L
# include <bits/c++0x_warning.h>
#else

so that if you don't have sufficiently new C++ standard, you get a compile error.

Now the problem is the CDT indexer that is used to generate highlighting and code completions does not know you are using __cplusplus >= 201103L . You can see this in this following screenshot that the majority of chrono is inactive because __cplusplus is the wrong value.

This screenshot shows the incorrect value and the errors CDT identifies:

计时头已禁用

And if you try and code complete, you get the wrong thing too:

缺少代码完成

How to fix it

To fix the problem, you need to tell CDT that your project uses GCC settings that are different from the default GCC settings. ie because of the different standard __cplusplus in particular has the wrong value.

  1. In Project Properties, choose C/C++ General -> Preprocessor Includes and then the Providers tab.
  2. Choose the CDT GCC Built-in Compiler Settings
  3. Uncheck the Use global provider shared between projects
  4. Press OK

Here is a screenshot of what that looks like:

非全局GCC发现设置

Once you do this, you should see that chrono 's inactive sections becomes correct in the editor:

计时

But your source file may still be wrong. You should then rebuild the indexes to update, right-click on the project, choose Index -> Rebuild :

重建索引

Finally your code should not display properly:

好突出

And the code complete should be working too!

代码完成后

History

This is a case where CDT's right hand and left hand don't agree. Historically I believe the reasoning for this is down to performance and trading off indexing every possible variant of compiler/user option, vs having some shared data across the workspace that may be wrong for some projects.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM