0

I am looking for a way to create a naming service. Basically I need a function that accepts anything as an argument and returns me the name of the given argument. This can be anything, class, function, variable etc.

std::string name(T t)
{
  if t is a function 
    return __func__ of t

  if t is a variable 
    return name of variable.
}

Any suggestions?

Montaldo
  • 863
  • 8
  • 16
  • 3
    And for temporary (without name) ? Not possible btw with function. MACRO may help. – Jarod42 Feb 25 '16 at 11:07
  • 1
    In C++ the *name* of a function or of a variable is just non sense. The name is only known at build time (compile & link) and later translated to an address. At run time all names have just vanished and cannot be knows - except when using special build mode to allow debuggers to keep track of original names. – Serge Ballesta Feb 25 '16 at 11:16
  • Right they are basically only there to establish a link and vanish afterwards. – Montaldo Feb 25 '16 at 11:17

4 Answers4

7

C++ is not the right language to do this, it has no reflection capabilities at all, and you can't treat "anything, class, function, variable etc." uniformly. You can't pass a class to a function, or pass a function to a function, they are not objects.

Jonathan Wakely
  • 166,810
  • 27
  • 341
  • 521
5

With MACRO, you may do

#define name(n) #n

which stringify given argument.

Jarod42
  • 203,559
  • 14
  • 181
  • 302
  • This basically returns me the name of the variable, thought if this is in a function scope it will return me the name in that specific scope doesnt it. Is there any way to retrieve the original name>? As in the name that it is given when declared – Montaldo Feb 25 '16 at 11:13
  • 1
    Use directly the macro, not in function – Garf365 Feb 25 '16 at 11:14
2

In C++ the name of a function or of a variable is just non sense. The name is only known at build time (compile & link) and later translated to an address. At run time all names have just vanished and cannot be knows - except when using special build mode to allow debuggers to keep track of original names.

What would be closer than that would be a function accepting a pointer to void:

std::string address(const void *t) {
    std::ostringstream os;
    os << "Address is " << t;
    return os.str();
}

You can then use it this way:

int i;
std::string s;
s = address(static_cast<const void *>(&i));
...
double d;
s = address(static_cast<const void *>(&d));
...
// if f is declared as int f(double d, std::string s):
s = address(static_cast<const void *>(&f));
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
  • Maybe VS2015 might have something to convert that memory adress to the original name. I am basically creating a tool for debugging purposes to keep track of the execution time of a some functions to find bottle necks in my algorithms. I want to store that function name to report the output – Montaldo Feb 25 '16 at 11:33
  • 1
    @Montaldo: you should have said that in your question! My advice is to leave this question *as is* because it would change it too much and make current answers off topic, and ask a new one, explaining that context. You could get answers explaining how to make use of the debugging information generated by a specific compiler. – Serge Ballesta Feb 25 '16 at 11:42
  • Thanks for the advice! – Montaldo Feb 25 '16 at 12:12
1

As answered already, C++ doesn't have reflection. But if you have debug symbols available at runtime different OS/compiler combinations make that information available - if you put enough effort into it.

Search for mechanisms to get the C++ stack trace or back trace.

E.g., this question has multiple answers that point to libraries that are useful for Linux, and separately for Windows: C++ display stack trace on exception (There are also other answers on SO and on the web in general.)

Community
  • 1
  • 1
davidbak
  • 5,775
  • 3
  • 34
  • 50