Extract the variable name of a pointer as a string - c++

I have a pointer to an int object.
int x = 10;
int *ptr;
ptr = &x;
I want to write a function GetString(int *ptr) which would return me the string ptr. How do I do that in C++?

There isn't a direct way to extract a variable's string name in C++. The language has only rudimentary reflection features, see the type_traits standard library and SO post Why does C++ not have reflection?.
But you can use preprocessor stringification # to hack something similar: define
#define STRINGIFY(x) #x
then the preprocessor substitutes STRINGIFY(ptr) with the string "ptr".
Note: if you want to stringify the result of a macro expansion use two levels of macros.

#include <iostream>
#include <map>
const std::string& getString(int *ptr, const std::map<int*, std::string>& mp){
return mp.find(ptr)->second;
}
int main()
{
std::map<int* , std::string> mp;
int x = 10;
mp.insert({&x, "ptr1"});
int y = 9;
mp.insert({&y, "ptr2"});
std::cout << getString(&y, mp);
}
Demo

Related

How to name an integer with a string name?

I want to have a function making a new integer like this:
void makeInt(string str, int x);
void makeInt(string str, int x) {
int str = x;
}
int main() {
makeInt(name,4);
}
But now the question is, how can I make the string, name the integer? So that int name = 4. Because now it would only give out an error. Is that actually possible?
This is by the way in C++
You can, use C preprocessor macro.
#define makeInt(str, x) int str = x
int main() {
makeInt(name, 4);
}
Other than that, there is no way to do that in C++.
But I can't see any reason why would you want to do that.
If you want to store a variable with name inputted from user, and then retrieve the variable with name inputted from user, consider using std::map<std::string, int>.
#include <map>
#include <iostream>
#include <string>
int main(){
std::map<std::string, int> values;
values["name"] = 4;
std::cout << values["name"];
}
will prints 4.

Autocasting C++ char array as string

Let's say I have a String class that can be constructed with a char array pointer. Is there any crazy way, through some magical operator overload, free function, or preprocessor macro to make Python-like syntax work, autocasting a char array literal to a String? To make this compile:
String a = "Foo".substr(1);
I suppose a wild pre-compile sed statement would do it, but something within the abilities of clang would be preferred.
For C++11 and beyond
#include <iostream>
#include <string>
int main() {
using namespace std::string_literals;
auto a = "foo"s.substr(1);
}
If you wanted to write this for your own String class then the way to get the same behavior would be to roll your own user defined string literal, and then do the same
#include <cstddef>
class Str {
public:
explicit Str(const char*) {}
Str substr(int) { return *this; }
};
Str operator"" _s (const char* input, std::size_t) {
return Str{input};
}
int main() {
auto s = "something"_s.substr(1);
}

Simple reflection mechanism

Is there any solution to run a function/macro that name is created by concatenation of two strings?
I just want to do something like that:
template <char *A, char *B, int C>
int function_that_run_other_function(void){
// Here is the main point
char function_name[80];
strcpy (function_name, "function");
strcat (function_name, A);
strcat (function_name, B);
return function_name(C);
}
I can do this using macro:
#define macro_that_run_function(A,B,C) \
function_##A##B##(C);
But I don't want to use macro because of many problems with macros.
Is it possible to do in C++ without any additional libraries?
I got curious and after a little while I ended up with this ungodly mess and general mainentace nightmare:
main.cpp:
#include <map>
#include <iostream>
#include <sstream>
#include <functional>
#include <cstring>
typedef std::function<void(int)> func;
typedef std::map<std::string, func> FuncMap;
template <char* A, char* B, int C>
void runner(FuncMap funcs){
std::stringstream ss;
ss <<A <<B;
return funcs[ss.str()](C);
}
void ABC(int val) {
std::cout <<"Woo: " <<val <<"\n";
}
extern char a[]; //due to external linkage requirement
extern char b[];
int main(...) {
FuncMap funcs;
strcpy(a, "A");
strcpy(b, "B");
funcs["AB"] = std::bind(&ABC, std::placeholders::_1);
runner<a, b, 0>(funcs);
return 0;
}
vars.cpp:
char a[5] = {""};
char b[5] = {""};
So yes with enough force you can make c++ do something along the lines of what you want, but I really wouldn't recommend it.
No, C++ does not allow the compile or run time manipulation or inspection of symbol names (barring the implementation specified type info stuff).
Dynamic libraries often export names (mangled for C++, almost unmangled for `extern "C"``), and libraries for loading them usually (always?) allow them to be loaded by string value.

C++ preprocesor macro for accumulating comma-separated strings

I need to do the following:
const char* my_var = "Something";
REGISTER(my_var);
const char* my_var2 = "Selse";
REGISTER(my_var2);
...
concst char* all[] = { OUTPUT_REGISTERED }; // inserts: "my_var1, my_var2, ..."
REGISTER and OUTPUT_REGISTERED are preprocesor macros. This would be great for large number of strings, like ~100. Is it possible to accomplish this?
PS. The code belongs to level-0 "block" – i.e. it is not inside any function. AFAIK, I cannot call regular functions there.
#include <iostream>
#include <vector>
using namespace std;
vector<const char*>& all()
{
static vector<const char*> v;
return v;
}
struct string_register
{
string_register(const char* s)
{
all().push_back(s);
}
};
#define REGISTER3(x,y,sr) string_register sr ## y(x)
#define REGISTER2(x,y) REGISTER3(x,y,sr)
#define REGISTER(x) REGISTER2(x, __COUNTER__)
REGISTER("foo");
REGISTER("bar");
int main()
{
}

Converting an int to std::string

What is the shortest way, preferably inline-able, to convert an int to a string? Answers using stl and boost will be welcomed.
You can use std::to_string in C++11
int i = 3;
std::string str = std::to_string(i);
#include <sstream>
#include <string>
const int i = 3;
std::ostringstream s;
s << i;
const std::string i_as_string(s.str());
boost::lexical_cast<std::string>(yourint) from boost/lexical_cast.hpp
Work's for everything with std::ostream support, but is not as fast as, for example, itoa
It even appears to be faster than stringstream or scanf:
http://www.boost.org/doc/libs/1_53_0/doc/html/boost_lexical_cast/performance.html
Well, the well known way (before C++11) to do that is using the stream operator :
#include <sstream>
std::ostringstream s;
int i;
s << i;
std::string converted(s.str());
Of course, you can generalize it for any type using a template function ^^
#include <sstream>
template<typename T>
std::string toString(const T& value)
{
std::ostringstream oss;
oss << value;
return oss.str();
}
If you cannot use std::to_string from C++11, you can write it as it is defined on cppreference.com:
std::string to_string( int value )
Converts a signed decimal integer to a string with the same content as what std::sprintf(buf, "%d", value) would produce for sufficiently large buf.
Implementation
#include <cstdio>
#include <string>
#include <cassert>
std::string to_string( int x ) {
int length = snprintf( NULL, 0, "%d", x );
assert( length >= 0 );
char* buf = new char[length + 1];
snprintf( buf, length + 1, "%d", x );
std::string str( buf );
delete[] buf;
return str;
}
You can do more with it. Just use "%g" to convert float or double to string, use "%x" to convert int to hex representation, and so on.
Non-standard function, but its implemented on most common compilers:
int input = MY_VALUE;
char buffer[100] = {0};
int number_base = 10;
std::string output = itoa(input, buffer, number_base);
Update
C++11 introduced several std::to_string overloads (note that it defaults to base-10).
The following macro is not quite as compact as a single-use ostringstream or boost::lexical_cast.
But if you need conversion-to-string repeatedly in your code, this macro is more elegant in use than directly handling stringstreams or explicit casting every time.
It is also very versatile, as it converts everything supported by operator<<(), even in combination.
Definition:
#include <sstream>
#define SSTR( x ) dynamic_cast< std::ostringstream & >( \
( std::ostringstream() << std::dec << x ) ).str()
Explanation:
The std::dec is a side-effect-free way to make the anonymous ostringstream into a generic ostream so operator<<() function lookup works correctly for all types. (You get into trouble otherwise if the first argument is a pointer type.)
The dynamic_cast returns the type back to ostringstream so you can call str() on it.
Use:
#include <string>
int main()
{
int i = 42;
std::string s1 = SSTR( i );
int x = 23;
std::string s2 = SSTR( "i: " << i << ", x: " << x );
return 0;
}
You can use this function to convert int to std::string after including <sstream>:
#include <sstream>
string IntToString (int a)
{
stringstream temp;
temp<<a;
return temp.str();
}
You might include the implementation of itoa in your project.
Here's itoa modified to work with std::string: http://www.strudel.org.uk/itoa/
Suppose I have integer = 0123456789101112. Now, this integer can be converted into a string by the stringstream class.
Here is the code in C++:
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n,i;
string s;
stringstream st;
for(i=0;i<=12;i++)
{
st<<i;
}
s=st.str();
cout<<s<<endl;
return 0;
}
#include <string>
#include <stdlib.h>
Here, is another easy way to convert int to string
int n = random(65,90);
std::string str1=(__String::createWithFormat("%c",n)->getCString());
you may visit this link for more methods
https://www.geeksforgeeks.org/what-is-the-best-way-in-c-to-convert-a-number-to-a-string/