Yuri Almeida
Yuri Almeida

Reputation: 139

Intellisense not working with templates VS2012 ultimate c++

Intellisense is working very badly with my VS...

When I'm coding in C++ it works 100% fine, until I start using templates, then it just stops working completely, stops detecting errors and don't auto-complete in all cases.

I'm inclined to believe it has to do with something I have done with my code that broke it.

So I started by creating a class with a struct in it:

template<typename T>
class List<T>
{
private:
    struct Node
    {
        T value;
        Node* next;
        Node* previous;
    };
    Node* First;
    Node* Last;
...
};

later, I declare some additional functions:

template<typename T>
inline T List::First()
{
    return First;
}

so far so good, intellisense is working 100% fine, but if I try to do first-> it won't work, VS won't tell give me any options (ctrl + space doesn't work).

also, if I type some nonsense it won't tell me it's wrong:

sdkjgisjdga->vsrsiib = 123jgi;

I don't really know what to do in order to fix this.

Thank you for your time and efforts.

PS: I already tried to reset the configurations.

EDIT: Forgot to say that if i don't use templates in my .h file then intellisense works correctly.

Upvotes: 7

Views: 3678

Answers (2)

RhetoricalRuvim
RhetoricalRuvim

Reputation: 278

I am a bit late with this answer, and maybe the OP is not working on this code anymore, but I think this may still help someone who is searching templates and IntelliSense.

One thing you can try -- if you want to be able to see your typos and errors as you type, but if you want your code to be template-able -- is using an #if-#else-#endif:

#if MY_USE_TEMPLATES 
template <typename T> 
#else 
typedef [**your-test-type-here**] T; 
#endif 
class List { 
... your code here ... 
} 

In Visual Studio 2015 this seems to work. You can define MY_USE_TEMPLATES to be 0 (using a #define), develop your code with IntelliSense and auto-complete, etc. (so you make less mistakes), and then change the MY_USE_TEMPLATES to 1 when you are ready to test or compile with actual template code.

While you have MY_USE_TEMPLATES turned on, code that references your List will result in error (i.e., code like 'List myList'), and you can resolve that with an extra dummy 'template' inside the #else statement. However, the good thing about leaving the #else clause without an extra 'template' is: the error you get when referencing your List may serve you as a reminder to turn on MY_USE_TEMPLATES before testing the code, reducing the probability of a bug. (Experience suggests that it is easy to forget when handling a lot of things and large projects ...)

Do be careful about using multiple such type definitions, however: the 'typedef ... T' can only be safely used once for that name "T"; while you can use 'typedef ... TYPE1' for one class and 'typedef ... TYPE2' for another, you cannot safely use the same type name for different types unless you put the different type names into separate namespaces. (I tried this in Visual Studio 2015.)

Upvotes: 10

MSalters
MSalters

Reputation: 179799

Templates need to be instantiated before you can definitively say what they contain. For example, your First-> snippet points to a List<T>::Node, and that is obviously dependent on the exact T.

Sure, Intellisense in this simple case could unambiguously list the members by just substituting T==int for a moment. But consider what's worse: Intellisense not knowing the members (like now) or Intellisense mis-guessing in the hard cases where you need it most?

Upvotes: 8

Related Questions