I am using eclipse with cdt (Neon.3 Release 4.6.3) as the IDE for C++.
Some code of mine is compiling and running fine, but I have issues with configuring eclipse.
In a header file, I have a template class, which looks like this
template <int dimen, int neighbours, typenameDATATYPE >
class Grid {
public:
get_solidify_cells(const int &pos)
private:
std::vector<MacroCell<dimen, neighbours, DATATYPE>> macrogrid;
};
int Grid<dimen, neighbours, DATATYPE>::get_solidify_cells(const int &pos) {
const MacroCell<dimen, neighbours, DATATYPE> cell = macrogrid[pos];
const auto cell1 = macrogrid[pos];
const auto cell2 = cell;
return cell.get_solidify_cells();
}
MacroCell
has a method get_solidify_cells
.
The resolution of the auto
, when hovering with the mouse over it fails for cell1
. It thus has no auto completion and F3
fails to jump to the definition of get_solidify_cells
Without auto
, as this is the case of cell
auto completion and F3
are working fine.
If I hover over the auto form cell1 I get const 1 127 108 14 MSCAFE::Grid 3 10 103 129 5 3 #0 0 10 103 129 5 3 #1 0 #2 MSCAFE::Grid::macrogrid 0 35 MSCAFE::Grid::get_solidify_cells 0 0 cell1
If I hover over the auto form cell2 I get const MacroCell<int3 #0 0,int3 #1 0,DATATYPE> cell2
C++11
integration works fine, emplace_back
can be resolved.
How can I configure eclipse, in order to fix this issue?
Here is a reduced piece of code (with different identifier names) that demonstrates the issue:
#include <vector>
template <typename T>
struct Waldo {
void find();
};
template <typename T>
void foo(const std::vector<Waldo<T>>& v) {
auto w = v[0];
w.find(); // would like auto-completion after "w." to offer "find"
}
Note that you are asking for auto-completion inside a template, and more specifically on an object whose type is dependent on a template parameter. This is a hard problem in general.
In today's world, it's reasonable to expect that an IDE can do everything that a compiler can, including knowing the types of variables defined using auto
. However, when it comes to templates, it's important to understand that compilers do very little processing of template bodies prior to instantiation.
Sure, in the above case, a compiler can deduce the type of the variable w
- for a specific instantiation of the function foo
, with a concrete type available to substitute for T
. However, it can't deduce the type of w
prior to instantiation - indeed, the deduced type can potentially be different for different values of T
. (And I don't just mean that Waldo<int>
is a different type from Waldo<double>
- thanks to C++'s ability to specialize templates, vector
could potentially be specialized for some Waldo<U>
such that its operator[]
returns a completely different type. Now you and I know that that won't happen, but there is no way for a compiler to know that.)
So, in asking the IDE to provide auto-completion after w.
- which requires knowing the type of w
- in an un-instantiated context, we are asking it to do something that compilers never need to do, and which can't be done accurately in general.
Now, IDEs (including Eclipse) do employ heuristics to try to give useful auto-completion results even in dependent contexts, on a best-effort basis. Unfortunately, these heuristics tend not to be able to see through the copious amounts of template metaprogramming present in typical standard library implementations. (As an exercise, open up your standard library's <vector>
implementation, and see how vector::reference_type
(the declared return type of operator[]
) is defined.)
@RichardCritten suggests using a different IDE than Eclipse. I do not expect that to help. Indeed, I invite anyone to show me a C++ IDE that does give accurate auto-completion (that's semantically correct, not just "all the identifiers found in the file") for the above code example.