In Eclipse, I’m looking at code which is essentially the same as the example posted in the codereview.SX question “C++17: Boost.Hana based compile-time plugin registration”, just condensed into one file and with the Boost stuff stripped out:
#include <iostream>
#include <type_traits>
template<class>
struct sfinae_true;
template<unsigned int>
struct registry_hook;
namespace registry {
template<class Dummy, unsigned int testN>
constexpr sfinae_true<decltype(registry_hook<testN>{})> test(int);
template<class Dummy, unsigned int testN>
constexpr std::false_type test(long);
template<class Dummy, unsigned int testN = 0>
constexpr unsigned int get_free_N() {
if constexpr (std::is_same<decltype(test<Dummy, testN>(0)), std::false_type>::value) {
return testN;
} else {
return get_free_N<Dummy, testN + 1>();
}
}
}
struct Dummy1;
template<>
struct registry_hook<registry::get_free_N<Dummy1>()> {
constexpr static unsigned int my_N = registry::get_free_N<Dummy1>();
};
struct Dummy2;
template<>
struct registry_hook<registry::get_free_N<Dummy2>()> {
constexpr static unsigned int my_N = registry::get_free_N<Dummy2>();
};
int main() {
constexpr unsigned int max_N = registry::get_free_N<void>() - 1;
std::cout << max_N << std::endl;
}
This compiles and works fine with both GCC and clang:
$ g++ -std=c++17 -Wall -Wextra -pedantic main.cc && ./a.out
1
$ clang++ -std=c++17 -Wall -Wextra -pedantic main.cc && ./a.out
1
However, Eclipse complains about the lines with explicit template specializations, giving the error “Invalid template argument”. The parser log file shows the same, without any more context:
Project: test File: file:[…]/main.cc Language: GNU C++ Index Version: 220.0 Build Configuration: Default Context: file:[…]/main.cc C++, {} Versions in Index: 1 C++: {}; 0 macros, 2 includes, 47 names; Include Search Path (option -I): /usr/include/c++/11 /usr/include/x86_64-linux-gnu/c++/11 /usr/include/c++/11/backward /usr/lib/gcc/x86_64-linux-gnu/11/include /usr/local/include /usr/include/x86_64-linux-gnu /usr/include Macro definitions (option -D): […] Unresolved names: A template id provides illegal arguments for the instantiation: registry_hook in file […]/main.cc:28 A template id provides illegal arguments for the instantiation: registry_hook in file […]/main.cc:34 Written on Tue Dec 05 23:01:27 EST 2023
I don’t see what the problem is supposed to be. Is this just a bug in Eclipse/CDT?