General
No one asked, but I've written a C++ header-only library so you don't have to declare the variable again.
0 views

In my spare time, I wrote a header-only C++ library that will help you extract variable declarations from the main(...) function of one .cpp file to the main(...) function of another .cpp file.
Yes, I mean you can declare any type of variable in any way you can think of, as long as it's within the main(...) function, from a custom-named struct to strange declarations like int a,b,c = 100; as long as it's syntax-correct in C++.
Quick Example :
#include<iostream>
#include<vector>
#include "var_saver.hpp"
int main(){
int a = 0;
std::vector<int>c;
double f,r=3,h;
//Save variables to a file
VarSaver::SaveVarToFile(__FILE__,"save.out");//or another name
// Load the variables into target file
VarSaver::LoadVarToFile("save.out","main2.cpp");
return 0;
}
main2.cpp before loading the variables:
#include<iostream>
#include<vector>
int main(){
return 0;
}
main2.cpp after loading the variables:
#include<iostream>
#include<vector>
int main(){
int a=0;
std::vector<int>c;
double f;
double r=3;
double h;
return 0;
}
Key advantage : You only need to call the function once—anywhere in your code, as long as it gets executed at least once during runtime.
Anyway, you can visit, check out, and try out this library.
Note: If you can't read the code in the repository, it's my fault, not yours. I'm a competitive programmer, so you might have some difficulty reading variable names and trying to understand their meaning.
Link repository: VarSaver
I'd love to hear your feedback and suggestions!
Read original: https://dev.to/meolaptrinh/no-one-asked-but-ive-written-a-c-header-only-library-so-you-dont-have-to-declare-the-variable-20ia
Related posts

How we halved DNS build delay and cut memory use by 85%
0Cloudflare Blog
Birdbuddy vs. Birdfy: Which Budget Smart Feeder Should You Buy? (2026)
0Wired
2 days till TechCrunch Founder Summit: Boston founders don’t need a Silicon Valley address to build something big
0TechCrunch
Adventurer returns home to Hull after 28-year walk from South America
0Hacker News New
Vending machine has terms and conditions you must accept before using [video]
0Hacker News New
Tomorrow Corporation Tech Demo [video]
0Hacker News New