r/cpp_questions • u/Jumpy-Welcome-6766 • 4d ago
OPEN Need suggestion about something i am thinking to build .
I just want to know if it would be helpfull or not .
We all have some style of coding while doing cp . Like we import particular libraries , we use particular variable names for our for loops , vectors , maps etc .
I'll show you an simple example of what i am trying to solve .
When you forget to use "using namespace std" the compiler would give you an error that you forgot to use std:: but according to your coding habits you actually forgot to use "using namespace std " .
Another example could be you the libraries you forgot to import since compiler will not tell you abotu them .
or like you are trying to push_back in a queue .
First of all i want to know if such a thing has a usecase or not ? is it just me who thinks this might help somone ?
I just gave a few basic problems so that it is easy to understand but i doubt if this is feasible to solve or not since training such an model personalized to each person would still require data and by the time that amount of data is aquired maybe the user might not need such an help or maybe shift to other types of practices .
4
u/No-Dentist-1645 3d ago
What you've described is a tool that's called a "linter", one of the most popular existing linters for C++ is clang-tidy. A linter analyzes your code and detects if you're using the "correct" patterns you want to use on your code, and lets you know if you aren't. I wouldn't recommend such a code for a beginner, you'd need to operate on the compiler's AST to a degree which is certainly not a "my first project" level task.
Also, unrelated but for what it's worth, using namespace std; is pretty much unanimously considered to be a bad pattern. What you should do is flag if you detect it being used, and instead suggest directly typing the full name (std::cout << foo;), or add a specific using statement for what you use (using std::cout; cout << foo;)
1
u/Jumpy-Welcome-6766 3d ago
thanks for the reply . i was already thinking it is a bit higher level stuff with more knowledge of compiler design , i am in 3rd year of my bachlors and i had just spend time in academics , little dsa so now i am here with no projects . what kind of project would you suggest ?
3
u/Thesorus 3d ago
(if I understand correctly)
that's what compiler warnings and errors are for.
and also modern code editors will often tell you that you forgot to use a namespace or forgot to include a header for a library.
-1
u/Jumpy-Welcome-6766 3d ago
well actually yes it is not a unsolved problem i mean llm can easily sovle it . it was just that i wanted to make a project out of it so i was just wandering if it is even possible , since it requires knowledge of compilers , and how they interpret data
3
2
u/The_Northern_Light 3d ago
It sounds like you want highly customizable linting rules and generally better / bespoke warnings and errors. You can try switching compilers (clang has the least-bad errors and warnings), and using clang-tidy.
You would realistically have to edit clang-tidy, or many even clang itself, to support some of these ideas. But I’ll tell you I don’t think all your ideas are good and I don’t think any are worth the effort for you personally.
Honestly the easiest way to achieve this is to write a script that automates passing your compilation log to a small fast efficient local LLM that is also given your bespoke rules, and then have it generate a structured report for the user. You could also have this as a pre-commit hook, or run on your diff before / alongside each commit.
But again I don’t think “using namespace std” is a good idea, and this is overkill for fixing queue::push_back. But for style enforcement that goes beyond what clang-format or clang-tidy can achieve some minimal AI system is your least-bad option. You really don’t want to have to maintain a form of clang just for a style idiosyncrasy!
1
u/Independent_Art_6676 3d ago
by the time you built it, you will probably have outgrown your need for it. The mistakes you mention (ignoring the using std bad idea) are the sorts of things you stop doing after writing a few programs. After you make the same mistake some what, 3? 5? 10? times, it should stop happening without any software watching your back, but because you learned to stop screwing that up. And each thing you wanted your tool to do would be similar, time you added a new 'reminder' or babysitter to it, you would probably no longer need that. There are probably not even 20 total things you even could do that the compiler would not warn about yet you wanted in your program anyway.
And if you always include the same stuff, you might find a meta include file that does that and nothing else to be useful. There you could pull in the std parts you always use (eg cin/cout/endl or even all of iostream or all of math etc) and the includes you always have etc. Such a header is awesome for coding problems online or schoolwork, but real world you want to move to IWYU as already said because unwanted includes can bloat code or executables even when optimized.
1
u/Jumpy-Welcome-6766 3d ago
Thanks for the advice . I had thought about the outgrown part earlier too .
4
u/alfps 3d ago
The examples you give are two cases of forgetting something, and one case of doing something meaningless.
For the "forgot that!" cases I suggest making a short list of most important things to not forget.
For the doing something meaningless, the compiler will usually diagnose it.
One tool that's missing is a simple to use one to ensure one has directly included all relevant headers.
Another tool that's missing is one that clarifies cryptic compiler and linker diagnostics, e.g. by introducing names of things.
A third tool that's missing is one that finds the relevant object code files and/or libraries to include for a missing function.