将函数指针类型转换为相应的字符串
Convert function pointer type to corresponding string
还没有
读过关于C++模板的书,所以我遇到了一个问题。我正在使用C++14。
如何根据类型本身将函数指针类型转换为特定字符串?我有函数指针类型:
using FuncType1 = int (*)(double);
using FuncType2 = int (*)(int);
using FuncType3 = int (*)(int);
我想写这样的东西:
class Test {
private:
template<FuncType1> static const std::string func_name = "FuncType1";
template<FuncType2> static const std::string func_name = "FuncType2";
template<FuncType3> static const std::string func_name = "FuncType3";
public:
template<T> std::string GetFuncType() {
return func_name<T>::value;
}
};
我知道这不会编译,但应该足以显示想法。我认为可以专门化GetFuncType
方法,但我更愿意专门化成员变量func_name
(如果可能的话(。另外 - 如果代码是固定的,FuncType2 和 FuncType3 会解析为正确的字符串吗?
我更愿意func_name专门化成员变量(如果可能的话(。
所以,如果我理解正确,你看起来是
class Test
{
private:
template <typename> static const std::string func_name;
public:
template <typename T>
std::string GetFuncType () const
{ return func_name<T>; }
};
template <typename> const std::string Test::func_name = "not func";
template<> const std::string Test::func_name<FuncType1> = "FuncType1";
template<> const std::string Test::func_name<FuncType2> = "FuncType2";
template<> const std::string Test::func_name<FuncType3> = "FuncType3";
鉴于FuncType1
,FuncType2
和FuncType3
是不同的类型(不等于FuncType2
和FuncType3
你的问题(。
以下是完整的编译示例
#include <string>
#include <iostream>
using FuncType1 = int (*)(double);
using FuncType2 = int (*)(int);
using FuncType3 = int (*)(long);
class Test
{
private:
template <typename> static const std::string func_name;
public:
template <typename T>
std::string GetFuncType () const
{ return func_name<T>; }
};
template <typename> const std::string Test::func_name = "not func";
template<> const std::string Test::func_name<FuncType1> = "FuncType1";
template<> const std::string Test::func_name<FuncType2> = "FuncType2";
template<> const std::string Test::func_name<FuncType3> = "FuncType3";
int main ()
{
std::cout << Test{}.GetFuncType<int>() << std::endl;
std::cout << Test{}.GetFuncType<FuncType1>() << std::endl;
std::cout << Test{}.GetFuncType<FuncType2>() << std::endl;
std::cout << Test{}.GetFuncType<FuncType3>() << std::endl;
}
如果希望该Test{}.GetFuncType<int>()
出现编译错误,则可以仅为请求的专用化初始化func_name
// template <typename> const std::string Test::func_name = "not func";
template<> const std::string Test::func_name<FuncType1> = "FuncType1";
template<> const std::string Test::func_name<FuncType2> = "FuncType2";
template<> const std::string Test::func_name<FuncType3> = "FuncType3";
// ...
std::cout << Test{}.GetFuncType<int>() << std::endl; // compilation error!
单向:
template<class T> constexpr char const* get_name() = delete;
using FuncType1 = int (*)(double);
using FuncType2 = int (*)(int);
template<> constexpr char const* get_name<FuncType1>() { return "FuncType1"; }
template<> constexpr char const* get_name<FuncType2>() { return "FuncType2"; }
int main() {
std::cout << get_name<FuncType1>() << 'n';
std::cout << get_name<FuncType2>() << 'n';
}
相关文章:
- 字符串函数在目标C++上运行时C++返回空
- 字符串函数返回奇怪的值
- 这里的字符串函数是什么意思
- 在字符串函数中抛出'char const*'实例后调用的终止
- 为什么 stl 字符串C++函数有时出错有时会出错?
- 从C++为字符串函数设置_ENV
- 来自 2D 数组的字符串函数
- 返回最长的公共子字符串函数 c ++
- 使用getline和字符串函数C++程序中的分段错误
- 在字符串函数的指针中返回地址
- 字符串函数:strcat()
- 这个子字符串函数有什么问题
- 字符串函数不返回字符串? C++
- 使用向量和字符串函数返回字符串
- 尝试修复一个字符串函数,该函数采用字符串并通过替换一些单词来更改它
- 逐字读取字符数组,无需字符串函数
- 我从我的字符串函数返回到主函数
- 为什么我不能从字符串函数返回字符串数组?
- 如何在不使用字符串函数的情况下在char数组中找到字符串?(C )
- 带有 #include< iostream.h> 的程序允许使用 strlen() 等字符串函数。为什么?