Search code examples
c++variadic-templatesexpandervariadicvariable-names

Expanding variable names in a variadic template


I'm not sure how to get access to the names of variables in a variadic template.

#define DebugVars(...) DEBUG_VARS(__FILE__, __LINE__, __FUNCTION__, ## __VA_ARGS__)
#define GetVarName(Variable) (#Variable)

void Log(const char* file, const int line, const char* func, const std::string& message)
{
    printf("file:%s, line:%d, func:%s \n%s", file, line, func, message.c_str());
}

template <typename... Args>
void DEBUG_VARS(const char* file, const int line, const char* func, Args&&... args)
{
    std::ostringstream ss;
    using expander = int[];
    (void) expander { 0, (void(ss << GetVarName(args) << ": " << args << "\n"), 0) ...}; 
    Log(file, line, func, ss.str());
}

void main()
{
    int number = 37;
    float pie = 3.14;
    std::string str = "test string";

    DebugVars(number, pie, str);
}

Output

file:main.cpp, line:29, func:main 
args: 37
args: 3.14
args: test string

Expected Output

file:main.cpp, line:29, func:main 
number: 37
pir: 3.14
str: test string

Example

DebugVars(...) is easy to drop into a function somewhere for debugging, but i'd need the variable names for it to be useful.


Solution

  • Bottom line you can't get the variable names in DEBUG_VARS, as the names are just identifiers that do not exist in your DEBUG_VARS function. However if you change your macro to also pass a stringyfied version of __VA_ARGS__ along with the arguments themselves, you can then tokenize them and use another stringstream in your fold expression, to print them out...

    #include <iostream>
    #include <sstream>
    
    #define DebugVars(...) DEBUG_VARS(__FILE__, __LINE__, __FUNCTION__, #__VA_ARGS__,__VA_ARGS__)
    
    void Log(const char* file, const int line, const char* func, const std::string& message)
    {
        printf("file:%s, line:%d, func:%s, message:%s \n", file, line, func, message.c_str());
    }
    
    template < typename... Args>
    void DEBUG_VARS(const char* file, const int line, const char* func, const std::string& names, Args&&... args)
    {
    
        std::stringstream names_ss;
        for (const char& c : names )
        {
            if (c == ','){
                names_ss << " ";
                continue;
            }
            names_ss << c;
        }
    
        std::string name;
        std::ostringstream ss;
        ss << "\n";
        using expander = int[];
        (void) expander { 0, (
              names_ss >> name, ss << name << ": " << args << "\n"
        ,0) ...};
        Log(file, line, func, ss.str());
    
    }
    
    int main()
    {
        int number = 37;
        float pie = 3.14;
        std::string str = "test string";
    
        DebugVars(number, pie, str);
        return 0;
    }
    

    Demo

    Obviously this only works with named arguments. and no nested function calls that have other arguments, else further processing of the string would be required.