2

I will like to be able to do something like this

int myVar = 3;

void logger(int param) {
  std::cout << nameOf(param) << ": " << param << std::endl;
}

logger(myVar); // prints "myVar: 3"
ellipticaldoor
  • 1,122
  • 11
  • 24
  • 1
    This is not possible with a function. Identifiers (the name of variables) do not exist at run time. You can try to achieve this with macros (`#define`) to construct a string literal based on an identifier at compile time. See [Stringification](https://stackoverflow.com/questions/16989730/stringification-how-does-it-work). – François Andrieux Nov 29 '20 at 19:11
  • Only possible with macros. – HolyBlackCat Nov 29 '20 at 19:11
  • How that macro will be? – ellipticaldoor Nov 29 '20 at 19:12
  • Something like `#define LOG(x) logger(x, #x)` ... assuming `void logger(int param, std::string const&)` or such. – Eljay Nov 29 '20 at 19:13
  • No, you cannot do what you asked. C++ does not work this way. – Sam Varshavchik Nov 29 '20 at 19:19
  • 1
    Does [how to print many variables with there name and their corresponding value in c++?](https://stackoverflow.com/a/64215959/7582247) answer your question? – Ted Lyngmo Nov 29 '20 at 21:23

2 Answers2

4

No, you can't do this in the C++ language yet, since there is no reflection facility to do this.

However, you can use preprocessor macros to achieve the effect you want. Note that macros are dangerous, and should be avoided as far as possible.

First, write an implementation function that takes the parameter value, and the parameter name, like this:

void logger_impl(int param, std::string param_name) {
  std::cout << param_name << ": " << param << std::endl;
}

Then you can write a macro that generates a string from the variable name in the call site, using # (the stringification operator), and then uses that string in the call to the implementation function:

#define logger(p) logger_impl(p, #p)

Here's a demo.

cigien
  • 57,834
  • 11
  • 73
  • 112
  • Was traumatized early in my career by a macro implementation of `strlen`. Soaked up half a day figuring out what happened during a time I didn't have half a day to spare. Compiler messages are better these days, but I also know how to read them better. – user4581301 Nov 29 '20 at 19:29
  • @user4581301 Yeah, I've had lots of small annoyances over the years. Can't stand macros really, and had to think for a few minutes if I even wanted to post an answer :p – cigien Nov 29 '20 at 19:31
  • I will use this only for debugging, no plans to commit this into the project. – ellipticaldoor Nov 29 '20 at 19:33
  • could be possible to allow to pass infinite number of variables? such as "logger(param1, param2)" – ellipticaldoor Nov 29 '20 at 19:33
  • I assume you mean arbitrary? Sure, you can have another macro that takes in multiple parameters, and calls this macro indirectly. – cigien Nov 29 '20 at 19:35
  • Do you know about an example to do it? – ellipticaldoor Nov 29 '20 at 19:40
  • 1
    Search for "variadic macros" and try solving it yourself. Shouldn't be too hard. – cigien Nov 29 '20 at 19:47
  • @cigien Its actually significantly harder to implement than a single argument version. I don't think you can achieve it without first implementing some form of macro for-each. – François Andrieux Nov 29 '20 at 20:39
  • @FrançoisAndrieux It's relative, but yeah, I probably made it sound easier than I should have. Still, no point dissuading the OP from trying it out themselves. – cigien Nov 29 '20 at 20:42
  • @FrançoisAndrieux Hmm, really? I've tried to forget everything I know about macros ;) but I think an additional variadic macro would suffice here. I could be wrong, and I'll have to write it out, to be sure. – cigien Nov 29 '20 at 20:43
  • I tried to implement it but failed, any suggestions? – ellipticaldoor Nov 29 '20 at 20:53
  • 1
    @ellipticaldoor If you've tried, and haven't found a solution, you can post a new question. – cigien Nov 29 '20 at 20:57
  • Calling macros "dangerous", particularly when you show a completely non-dangerous example, is a bit of a reach. – Asteroids With Wings Dec 05 '20 at 16:59
  • @AsteroidsWithWings Yeah, I see what you mean, I didn't have the disclaimer in the original answer. OTOH it's hard to tell when they're actually dangerous or not. And when they go wrong, it's really bad, so I do see the point of view that says warn up front. – cigien Dec 05 '20 at 17:08
  • \*shrug\* I've never had a "really bad" experience. Maybe your macros were truly awful ;) – Asteroids With Wings Dec 05 '20 at 17:14
  • @AsteroidsWithWings Well, my macros have always been absolutely perfect, it's other people's macros that are the problem ;) – cigien Dec 05 '20 at 17:15
0

Yes, it is possible!

Look at the following example:

#include <string> 

// add the following macro
#define NAMEOF(x) #x

int main(){
    int myvar{ 5 };
    std::cout << NAMEOF(myvar) << "=" << myvar << std::endl; // myvar=5
 
   // it could also be stored in a variable 
    std::string myvarname{ NAMEOF(myvar) };

    return 0;
}
Dorian Andrés
  • 211
  • 2
  • 3