Zeb*_*ish 3 c++ generics templates c++11
我有一个针对某些特殊情况重写的类,但我想知道是否可以使用C++泛型编程来决定函数包含的内容:
#include <iostream>
#include <typeinfo>
#include <string>
void printString(const std::string& str) { std::cout << str.c_str() << '\n'; }
template <typename T_callable>
struct FuncResultToString
{
FuncResultToString(T_callable func) : call(func) {}
T_callable call;
void turnFuncResultToString()
{
std::string str = "Type: ";
str += typeid(decltype(call())).name();
str += " / Value: ";
// IF RETURN TYPE IS CHAR* OR STRING
str += call();
// ELSE WILL HAVE TO TURN TO STRING FIRST
str += std::to_string(call());
printString(str);
}
};
double afunction() { return double(5.0); }
int main()
{
FuncResultToString<decltype(&afunction)> foo1(afunction);
foo1.turnFuncResultToString();
auto lambda = []() { return int(7); };
FuncResultToString<decltype(lambda)> foo2(lambda);
foo2.turnFuncResultToString();
}
Run Code Online (Sandbox Code Playgroud)
打印出:
Type: double / Value: 5.000000
Type: int / Value: 7
Run Code Online (Sandbox Code Playgroud)
这对许多类型都很好,但是在callable返回char指针或std :: string的情况下,我不想调用std :: to_string(),我只想按原样使用该值.有办法做到这一点吗?
你可以有过载:
const char* my_to_string(const char* s) { return s; }
const std::string& my_to_string(const std::string& s) { return s; }
template <typename T> std::string my_to_string(const T& s) { return std::to_string(s); }
Run Code Online (Sandbox Code Playgroud)
然后:
void turnFuncResultToString()
{
std::string str = "Type: ";
str += typeid(decltype(call())).name();
str += " / Value: ";
str += my_to_string(call());
printString(str);
}
Run Code Online (Sandbox Code Playgroud)