【发布时间】:2014-01-30 07:17:26
【问题描述】:
以下TestClass 有效:
#include <iostream>
#include <boost/function.hpp>
#include <boost/bind.hpp>
void ext_fun(const float f, int i)
{
std::cout << f << '\t' << i << std::endl;
}
template <typename T>
class TestClass
{
public:
boost::function <void (const T)> test_fun;
};
int main()
{
TestClass<float> tt;
tt.test_fun = std::bind(ext_fun, std::placeholders::_1, 10);
tt.test_fun(2.1);
return(0);
}
但是,我更愿意将test_fun 定义为成员函数模板,即类似
class TestClass
{
public:
template <typename T> boost::function <void (const T)> test_fun;
};
但是如果我这样做,我会得到这个编译器错误:“错误:数据成员‘test_fun’不能是成员模板”
是否可以使用boost::function 定义成员函数模板?如果是,怎么做?
谢谢
--马特奥
【问题讨论】:
-
抱歉,这没有意义——这不是“函数模板”的意思。你问的是“我能把
struct Foo { int a; };变成struct Foo { template <typename T> T a; };吗”,你不能。
标签: c++ templates boost boost-function