将函数指针类型转换为相应的字符串

Convert function pointer type to corresponding string

本文关键字:字符串 函数 指针 类型转换      更新时间:2023-10-16
还没有

读过关于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";

鉴于FuncType1FuncType2FuncType3是不同的类型(不等于FuncType2FuncType3你的问题(。

以下是完整的编译示例

#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';
}