Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/147.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如果函数返回值与模板参数相同,如何将函数作为模板参数传递?_C++_Templates_Function Pointers - Fatal编程技术网

C++ 如果函数返回值与模板参数相同,如何将函数作为模板参数传递?

C++ 如果函数返回值与模板参数相同,如何将函数作为模板参数传递?,c++,templates,function-pointers,C++,Templates,Function Pointers,我正试图弄清楚如何创建一个模板类,其工作原理如下: template<typename T, typename Fn> class Point { public: T x; T y; void pt(const std::string& x_str, const std::string& y_str) { x = Fn(x_str); y = Fn(y_str); } ....

我正试图弄清楚如何创建一个模板类,其工作原理如下:

template<typename T, typename Fn>
class Point
{
public:
    T x;
    T y;

    void pt(const std::string& x_str, const std::string& y_str)
    {
          x = Fn(x_str);
          y = Fn(y_str);
    }
    .... more Point stuff ....
};

typedef Point<float, std::stof> FPoint;
typedef Point<int, std::stoi> IPoint;
.....
到目前为止,我已经尝试直接定义,正如我上面所说,我已经尝试:

template<typename T, T (C*)(const std::string&)>
.....

然后做: 类型定义点浮点

我得到了各种错误,例如无法从“重载函数”转换为“float\u cdecl*const std::string&”或“std::stof”不是参数“convert\u func”的有效模板类型参数,具体取决于我如何定义参数。任何提示-我在谷歌上搜索到的似乎都没有帮助


谢谢

我想我已经弄明白了-std::stof实际上是std::stofconst字符串&,size\u t*。我把它放在模板参数tc*const std::string&,size\u T*中,现在在我使用Point的速度中出现错误,因为没有足够的参数。在我修正之后,如果我仍然错了,我将返回并删除此答案。

将第二个参数设置为函数指针: 样板 类点 {};

请注意,std::stof或std::stoi都不是该模板的有效参数。它们都接受其他参数,但创建依赖默认参数的包装器相当容易。模板参数必须完全匹配类型,而调用不: 类型定义点F点

您不能使用

typedef Point<float, std::stof> FPoint;
但这不起作用,因为std::stof有一个类型为std::size\u t*的可选参数。std::stof的类型,将std::string const&作为第一个参数是float*std::string const&,std::size\u t*

如果你试图用鞋钉点来匹配那个声明

template<typename T, T(*Fn)(const std::string&, std::size_t*)>
class Point { ... };

是的,就是这样。。。。显然,因为它是模板化的,所以我必须使用所有参数,即使std::stof被定义为std::stofconst std::string&,size\u t*=0
typedef Point<float, std::stof> FPoint;
template<typename T, T(*Fn)(const std::string&)>
class Point { ... };
template<typename T, T(*Fn)(const std::string&, std::size_t*)>
class Point { ... };
typedef Point<int, std::stoi> IPoint;
#include <cstdlib>
#include <string>
#include <functional>

template<typename T, T(*Fn)(const std::string&)>
class Point
{
public:
    T x;
    T y;

    void pt(const std::string& x_str, const std::string& y_str)
    {
       x = Fn(x_str);
       y = Fn(y_str);
    }
};

namespace MyApp
{
   float stof(std::string const& in)
   {
      return std::stof(in);
   }

   int stoi(std::string const& in)
   {
      return std::stoi(in);
   }
}

typedef Point<float, MyApp::stof> FPoint;
typedef Point<int, MyApp::stoi> IPoint;

int main()
{
   FPoint a;
   a.pt("100.25", "200.25");
   IPoint b;
   b.pt("100", "200");
}