Search code examples
c++17eclipse-cdt

Eclipse CDT is reporting false erros while using std::index_sequence


UPDATED: Added complete example and compiler information

I have Eclipse 2019-03 (4.11.0) with CDT 9.7.0.20190309 and the build-in compiler reports false positive errors while using std::index_sequence in C++17:

#include <gtest/gtest.h>
#include <utility>
#include <array>

class Sample {
public:
    template<std::size_t N >
    std::size_t get_percentage( void ) {
        return N;
    }

    template<std::size_t... Is>
    inline std::array<std::size_t, sizeof...(Is)> calculate_percentages( std::index_sequence<Is...> ) noexcept {
        return { this->get_percentage<Is>()... };
    }
    template<std::size_t N>
    inline std::array<std::size_t, N> get_percentages( void ) noexcept {
        return this->calculate_percentages( std::make_index_sequence<N>() );
    /*           ^^^^^^^^^^^^^^^^^^^^^ : Invalid arguments ' Candidates are: std::array calculate_percentages(std::integer_sequence) ' */
    }
};

TEST( IntegerSequence, InvalidArgumentsError ) {
    Sample test;
    std::array<std::size_t, 5> data = test.get_percentages<5>();
    for( int i = 0; i < 5; i++ ) {
        std::cout << data[i] << std::endl;
    }
}

int main( int argc, char ** argv ) {
    testing::InitGoogleTest( &argc, argv );
    return RUN_ALL_TESTS();
}

But the normal compilation succeeds without any problem.

My CDT GCC Built-in Compiler Settings in Project Properties -> C/C++ General -> Preprocessor Include Paths, Macros etc. -> Providers is as follows:

${COMMAND} ${FLAGS} -E -P -v -dD -std=c++17 "${INPUTS}"

The same applies for CDT Cross GCC Built-in Compiler Settings.

Rebuilding the index does not helped in there.

The GCC version I am using:

gcc (Ubuntu 8.3.0-6ubuntu1) 8.3.0

Many thanks in advance to anyone willing to help...


Solution

  • The problem is caused by the fact that the standard library that comes with gcc 8 and newer uses a new compiler intrinsic called __integer_pack to implement std::make_integer_sequence (and related utilities like std::make_index_sequence).

    Eclipse CDT's parser does not currently understand the __integer_pack intrinsic, and therefore it has trouble correctly parsing code that uses std::make_integer_sequence / std::make_index_sequence with these newer gcc versions.

    I filed a CDT bug to track adding support for the __integer_pack intrinsic.

    Meanwhile, a workaround you could employ is to use gcc 7 or older. If you need gcc 8 or newer to actually build your code, you could still tell Eclipse the look at the standard library headers of e.g. gcc 7 by replacing ${COMMAND} in the mentioned "built-in compiler settings" configuration with g++-7.

    UPDATE: The Eclipse bug is now fixed, with the fix targeting CDT's 9.11 release (scheduled to be part Eclipse 2020-03).