Skip to content

Instantly share code, notes, and snippets.

@shipof123
Last active July 5, 2018 14:21
Show Gist options
  • Save shipof123/7cf57c952054b1c79dda877844f9cb18 to your computer and use it in GitHub Desktop.
Save shipof123/7cf57c952054b1c79dda877844f9cb18 to your computer and use it in GitHub Desktop.
UDL for converting ints into std::strings and char * (online link http://cpp.sh/8bju7)
#include <deque> // if you use vectors instead you have to add them to the string in reverse order (no push_front())
#include <string>
#include <iostream> // for testing types
#include <cxxabi.h> // ditto
#include <typeinfo> // also for testing types
char * Demangle(const char* Object)
{
int status;
char * RealName;
RealName = abi::__cxa_demangle(Object, 0 , 0, &status);
return RealName;
}
std::string operator"" _str(unsigned long long int num)
{
std::deque<char> digits;
for(char i = 0; num != 0;num = num / 10)
{
i = '0'+(num % 10);
digits.push_front(i);
}
std::string converternum;
for(auto i = 0; i< digits.size();i++)
converternum += digits[i];
return converternum;
}
char * operator"" _cstr(unsigned long long int num)
{
std::deque<char> digits;
for(char i = 0; num != 0;num = num / 10)
{
i = '0'+(num % 10);
digits.push_front(i);
}
std::string converternum;
for(auto i = 0; i< digits.size();i++)
converternum += digits[i];
char * string = (char *)converternum.c_str();
return string;//converternum.c_str();
}
int main()
{
auto cstr = 213_cstr;
auto str = 213_str;
std::cout << Demangle(typeid(str).name()) << std::endl << Demangle(typeid(cstr).name()) << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment