DEV Community

Nguyen Duc Minh Quan
Nguyen Duc Minh Quan

Posted on

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

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;
}
Enter fullscreen mode Exit fullscreen mode

main2.cpp before loading the variables:

#include<iostream>
#include<vector>
int main(){
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

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;
}
Enter fullscreen mode Exit fullscreen mode

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!

Top comments (0)