以下TestClass
作品:成員函數模板使用boost ::功能
#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
?如果是,如何?
謝謝
--Matteo
對不起,這沒有意義 - 這不是「功能模板」的意思。你問的問題相當於「我可以把'struct Foo {int a;};'轉換爲'struct Foo {template T a;};'」,你不能。 –
@Kerrek SB我指的是像第一個例子[這裏](http://msdn.microsoft.com/en-us/library/swta9c6e%28VS.80%29.aspx),或者問題[here ](http://stackoverflow.com/questions/972152/how-to-create-a-template-function-within-a-class-c),但使用'boost :: function' –