converting a variable name to a string in C++

前端 未结 8 986
广开言路
广开言路 2020-12-01 01:15

I\'d like to output some data to a file. For example assume I have two vectors of doubles:

vector data1(10);
vector data2(10); 
         


        
相关标签:
8条回答
  • 2020-12-01 01:57

    I'd have thought the obvious answer is to make the function that performs the output take the heading text as a string parameter.

    0 讨论(0)
  • 2020-12-01 01:59

    I had the same problem. After a little bit of experimentation I created following macros that convert names of variables, fields, functions, methods and types to strings.

    #define MACRO_VARIABLE_TO_STRING(Variable) (void(Variable),#Variable)
    
    #define MACRO_FUNCTION_TO_STRING(Function) (void(&Function),#Function)
    
    #define MACRO_METHOD_TO_STRING(ClassName,Method) (void(&ClassName::Method),#Method)
    
    #define MACRO_TYPE_TO_STRING(Type) (void(sizeof(Type)),#Type)
    

    The code uses comma operator and void conversion to force compiler to check if variable, function, etc. really exists. The nice thing is that it works well with uninitialized variables too. I tested it on both VC and GCC with all pedantic options I found out without any warning messages.

    int GetAndPrintValue(const char* VariableName)
    {
       std::cout << VariableName << std::endl;
       return 10;
    }
    
    int Variable=GetAndPrintValue(MACRO_VARIABLE_TO_STRING(Variable));
    

    I use such code when I write parsers that reads data from input stream and if parsed variable is out of bounds it throws an exception with name of variable that failed my validity checks.

    0 讨论(0)
  • 2020-12-01 02:01

    Slightly adapted from @sarnold's answer, for C++:

    #define DEBUG(x) std::cout << #x << " = " << x << std::endl;
    

    An example program which uses this:

    int main() {
        int foo = 1;
        DEBUG(foo);
    
        return 0;
    }
    
    0 讨论(0)
  • 2020-12-01 02:01

    I had a similar quest. In Qt, I got tired of constantly writing the variable name as a string without autocomplete when writing to qDebug(). After a lot of trial and error with different macros and functions, I found that this macro works great:

    #define PRINT(x) ", " << #x << ": " << x
    

    Example usage:

    int someVariable = 42;
    double anotherVariable = 13.37;
    qDebug().nospace() << "Some text" << PRINT(someVariable) << PRINT(anotherVariable);
    

    Output:

    Some text, someVariable: 42, anotherVariable: 13.37
    

    I guess this (or something very similar) will work for std::cout as well.

    A bit late to the party, but I hope this can help anyone out there!

    0 讨论(0)
  • 2020-12-01 02:02

    You can use the preprocessor "stringify" # to do what you want:

    #include <stdio.h>
    
    #define PRINTER(name) printer(#name, (name))
    
    void printer(char *name, int value) {
        printf("name: %s\tvalue: %d\n", name, value);
    }
    
    int main (int argc, char* argv[]) {
        int foo = 0;
        int bar = 1;
    
        PRINTER(foo);
        PRINTER(bar);
    
        return 0;
    }
    
    
    name: foo   value: 0
    name: bar   value: 1
    

    (Sorry for printf, I never got the hang of <iostream>. But this should be enough.)

    0 讨论(0)
  • 2020-12-01 02:05

    try this:

    #define GET_VARIABLE_NAME(Variable) (#Variable)
    

    //in functions

    int var=0;    
    char* var_name= GET_VARIABLE_NAME(var);
    
    0 讨论(0)
提交回复
热议问题