Search code examples
c++eclipseeclipse-cdtvariant

Eclipse CDT does not parse <variant> include file correctly


I know, screenshots should not be provided as MCVE but here I want to point out the error.

enter image description here

Moving the focus into the include file I see then the problem:

enter image description here

I have set up Eclipse in that way that it folds all inactive preprocessor directives. So, it is clear Eclipse does not have defined the correct value for preprocessor #define __cplusplus [the - for Eclipse - inactive code has also a gray background, line #34].

I am using gcc 7.3 with -std=c++17.

What am I missing and how can I set this __cplusplus to the proper value? This small MCVE of course compiles.

Technical data:

Eclipse IDE for C/C++ Developers

Version: Oxygen.3a Release (4.7.3a)

Build id: 20180405-1200

Eclipse does not support apparently C++17 natively so I had to set in Project -> Properties -> C/C++ Build -> Settings -> GCC C++ Compiler -> Dialect -> Other dialect flags -std=c++17


Solution

  • The first hurdle is to get Eclipse to recognize that the code is being compiled in C++17 mode.

    There are several ways to do this, but the one I've found to be most reliable is to go to Project Properties -> C/C++ General -> Preprocessor Include Paths, Macros etc. -> Providers tab -> CDT GCC Built-in Compiler Settings and add -std=c++17 to the "Command to get compiler specs". Then do Project -> C/C++ Index -> Rebuild.

    That should get your trivial example to resolve without errors:

    #include <variant>
    
    std::variant<int, double> foo;
    

    However, as soon as you try to call a function on the variant:

    #include <variant>
    
    std::variant<int, double> foo;
    
    std::size_t i = foo.index();
    

    the function call index() in this case is marked as an error.

    This is because Eclipse's parser doesn't understand most C++17 features yet. In particular, it can't parse fold expressions, which are used fairly heavily in the definition of libstdc++'s (the standard library that ships with GCC) implementation of variant. As a result, Eclipse can't parse the variant class definition and thus does not know what members variant has.

    A workaround, as you've discovered, is to use @suppress to suppress the false-positive errors you get as a result. Note that you will also suffer some impairment in terms of editor features, such as not getting code completion on objects whose type is variant.

    You could consider contributing to Eclipse's C++ parser (this bug tracks support for fold expressions).