How i can get variable name in c++ without define [duplicate] - c++

This question already has answers here:
converting a variable name to a string in C++
(8 answers)
Closed 2 years ago.
I'm looking for a function that returns a variable name
Theoretical example
template <typename Type>
std::string GetVarName(Type Var)
{
//Get Name
return Variable_name;
}

You could try to stringify the variable using the preprocessor. In the posted link by Schultke there are some examples using macro in order to achieve that.

Related

What does (void) inside of a function does? [duplicate]

This question already has answers here:
Why cast unused return values to void?
(10 answers)
What does casting to `void` really do? [duplicate]
(4 answers)
Closed 5 months ago.
I was working and suddenly I faced a function like that:
void func(SomeStruct *parameter) { (void)parameter; }
I could not wrap my head on what it does.
I mean, there is a function func which the only thing it does is (void)parameter; but I can't get what (void)parameter; is doing int this example.
Would someone mind to explain me, please?

Different ways of declaring a function in C++ [duplicate]

This question already has answers here:
How does this template magic determine array parameter size?
(3 answers)
Can someone explain this template code that gives me the size of an array? [duplicate]
(4 answers)
How does this "size of array" template function work? [duplicate]
(1 answer)
Closed 2 years ago.
I have come across the following code snippet in C++ and I have no idea what it means;
#include <iostream>
char (&some_function(int (&some_input)));
int main ()
{
// main code here
return 0;
}
How is some_function a function? what kind of syntax is this? where is the body?
I'm sure this is only a C++ syntax as it doesn't compile in C.
Edit:
Actual code with above style:
template <typename T, size_t N>
char (&ArraySizeHelper(T (&array)[N]))[N];
#define arraysize(array) (sizeof(ArraySizeHelper(array)))

Set variable value making reference to it by a string [duplicate]

This question already has answers here:
Convert string to variable name or variable type
(7 answers)
Closed 5 years ago.
If the title is not so suggestive I hope the explanation might help you understand my problem.
I have a function which sets some variables value. But the variables name I would like to provide it as a string, this way:
void setValue( const std:: string& variable_name, int value){ variable_name=value;}
then when I call this function like this:
setValue("variable", 10);
I will expect to execute set variable=10;
Any idea if this approach is possible or other ways I could have this behaviour?
Thanks!
It is not possible to magically retrieve a variable from a run-time string. You need to provide a mapping in advance, which is probably not what you want. E.g.
int& getFromName(const std::string& s)
{
if(s == "variable") return variable;
if(s == "foo") return foo;
// ...
}

How to set variable name as a string? c++ [duplicate]

This question already has answers here:
How to adress variable by name stored in another variable (C++)
(2 answers)
Closed 5 years ago.
std::string VariableName = "name";
int (VariableNameHere) = 5;
From my understanding of c++ what I am asking is most likely impossible. If it is please post possible alternative solutions. Thanks!
As you have it is not possible, you would need to have some kind of look-up system, such as:
std::map<std::string, int> variables;
...
variables["name"] = 5;

C++: meaning of std::ostringstream& var [duplicate]

This question already has answers here:
What is a reference variable in C++?
(12 answers)
Closed 7 years ago.
I want to implement a function with a parameter that will receive a string result:
bool function (entry parameter, std::ostringstream & var)
I'm not sure about what it is, pointer ?
Should I do something special about it or just: var << result ?
The return value is boolean but we will need var after
& means it is a reference, not a pointer. See Andrew's comment link. You do not need to delete var, if that is what you mean by "something special." Given the method signature you provided, the code:
var << result;
should work just fine, assuming "result" is something sane. If you need more information, you may need to post a MCVE.