Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't VS autocomplete when I am using templates?

I am working on some data structures in C++ using templates and I ran to some problems.I am implementing a HashTable using chaining and I also implemented a Linked List.The first problem I ran was a Linking Error with my template definitions which I solved by including the .cpp file where the declaration was.

Now I ran to another issue and I am not sure if it is a problem related to templates or maybe VS settings.First of all,building the project doesn't update it unless I save and compile my entry point for some reason.I read some issues and the settings seem ok. The most important issue I ran into was that in my Hashtable.cpp file I have autocomplete issues regarding ONLY anything related to my List class.

For example : I used List<T> **arr in my Hashtable class and whenever I try to use any of the list methods e.g. arr[1]->insert(100) not only it doesn't autocomplete,but it says the the method belongs to as I show to the screenshot below. The declarations look like this : Hashtable.h:

#pragma once
#include "Log.h"
#include "List.h"
#include <vector>
#include <iostream>

Hashtable.cpp: #include "Hashtable.h"

And my list class doesnt have anything special included besides iostream etc

I want to point out that the code doesn't autocomplete but It works,it runs the correct methods but It is really uncomfortable and annoying to work with.Also I have worries that it might not work in another computer which really matters to me. Do you have any idea what could be wrong?

The screenshot I mentioned

like image 321
laegirl Avatar asked Sep 05 '25 17:09

laegirl


1 Answers

Autocomplete gets confused by templates. Keep in mind that when a compiler is compiling temnplated code, it knows what the template parameters are going to be. In contrast, when you're writing code in a templated class, the autocomplete system doesn't know what the parameter is going to be.

For instance, if you have List<T> **arr and List has an insert() method, is arr[1]->insert(100) valid? Maybe! or maybe not. There might be a specialization of List for a particular T, which doesn't have an insert method.

Additionally, autocomplete systems tend to be as much of an art as a science, since they have to run at interactive speed, and need to be robust to syntax errors earlier in the code. Templates put them at their worst, since the syntax and grammar of templates is considerably trickier to get right than other areas of C++, so when working with heavily templated code you'll tend to see autocomplete acting its worst.

When working with heavily templated code, I have on occasion "de-templated" the code while working on it simply to help the compiler out. Recent versions of VS will actually help you with this: templates that the compiler finds confusing may (usually, but not always, and I'm not sure why) offer you the opportunity to help it out:

enter image description here

If you click on that <T> icon, it'll ask you to give it an example of a type you might instantiate it with, and use that type for autocomplete. That can significantly improve its effectiveness.

like image 71
Sneftel Avatar answered Sep 07 '25 08:09

Sneftel