add number to std::string funtion
This commit is contained in:
parent
a9d8fa797f
commit
012d44d958
23
include/toolkit.h
Normal file
23
include/toolkit.h
Normal file
@ -0,0 +1,23 @@
|
||||
#ifndef TOOLKIT_H
|
||||
#define TOOLKIT_H
|
||||
|
||||
#include <charconv>
|
||||
#include <expected>
|
||||
|
||||
//use for to_chars
|
||||
constexpr size_t buffer_size = 32;
|
||||
|
||||
namespace toolkit{
|
||||
//number to std::string;
|
||||
template<typename T>
|
||||
std::expected<std::string, std::string> itos(T value){
|
||||
char buffer[buffer_size];
|
||||
auto res = std::to_chars(buffer, buffer+buffer_size, value);
|
||||
if(res.ec != std::errc()){
|
||||
return std::unexpected(std::make_error_code(res.ec).message());
|
||||
}
|
||||
return std::string(buffer, res.ptr - buffer);
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
@ -1,9 +1,11 @@
|
||||
#include <iostream>
|
||||
|
||||
#include <toolkit.h>
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
cout << "hello world!" << endl;
|
||||
double num = .2;
|
||||
auto str = toolkit::itos(num);
|
||||
cout<<str.value()<<"\n";
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user