Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt Creator's autocomplete (intellisense) doesn't work for std::vector elements

How can I make autocomplete in Qt Creator work for std::vector? Is it normal for it not to work?

For example, on a fresh new project, I create a struct foo { int bar; };. If I create a QVector of foo, intellisense/autocomplete works fine:

enter image description here

But for an std::vector<foo> v2 nothing happens after I press the dot in v2[0].

I'm on Qt Creator 3.3.0, using the Visual Studio compiler tool-chain (so the STL comes from VS, not gcc if that makes any difference).

Edit: I've found a related bug reported (about iterators though) - https://bugreports.qt.io/browse/QTCREATORBUG-1892. I reproduce this problem as well.

Edit 2: I tested with a custom template class:

struct bar {
    int b;
};
template<class T> struct foo {
    T operator [](int a) { return T(); }
};

And it's working fine:

enter image description here

like image 683
sashoalm Avatar asked Feb 06 '15 10:02

sashoalm


2 Answers

As per my comments, it's simply not possible to do this. You can file a bug report and hope they get it fixed. To explain better, it has something to do with std::vector's implementation:

reference operator[](difference_type _Off) const
    {   // subscript
    return (*(*this + _Off));
    }

In which reference is typedef'ed as Allocator::reference. Apparently Qt Creator has problems following this to the original class. Compare that with QVector's...

inline T &QVector<T>::operator[](int i)
{ Q_ASSERT_X(i >= 0 && i < d->size, "QVector<T>::operator[]", "index out of range");
  return data()[i]; }

...which is defined directly in terms of T&, and you can see why it works for it.

Update: Looking in cppreference's page on vector, it seems that, after C++11, reference should be typedef'ed as simply value_type&. I have tried building with CONFIG += c++11 and it still doesn't work, however.

And another update: Managed to code a minimal test case which doesn't work

template<typename T>
class foo{
public:
    typedef T value_type;
    typedef value_type& reference;
    T a;
    reference operator[](int){return a;}
};

struct bar{int b;};
like image 70
Cássio Renan Avatar answered Oct 23 '22 11:10

Cássio Renan


Now the clang code model is available:

Go to Help-> About Plugins... -> enable ClangCodeModel

Restart Qt Creator. Check that it got activated; Tools -> C++ -> Code Model

Voila code autocomplete for std:vector.

like image 22
newandlost Avatar answered Oct 23 '22 11:10

newandlost