2014-01-11 15 views
0

다음 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? 그렇다면 어떻게?

--Matteo

+0

죄송합니다. 의미가 없습니다. '기능 템플릿'의 의미가 아닙니다. 당신은 "struct Foo {int a;};를'struct Foo {template T a;};'"로 변환 할 수 있습니까? –

+0

@Kerrek SB 나는 첫 번째 예제 [here] (http://msdn.microsoft.com/en-us/library/swta9c6e%28VS.80%29.aspx) 나 [여기에있는 질문 ] (http://stackoverflow.com/questions/972152/how-to-create-a-template-function-within-a-class-c),하지만 부스트 :: 함수를 사용하여 –

답변

0

Is it possible to define a member function template using a boost::function ? If yes, how?

난 당신이 혼란 여기에가는 조금 생각 감사드립니다. 함수 템플릿은 무엇보다도 함수입니다. test_fun은 함수가 아니며 클래스 TestClass의 멤버 객체입니다. C++에서는 멤버 개체를 템플릿으로 만들 수 없습니다.

+0

좋아, 그래서 포인트 클래스 메서드는 템플릿화할 수 있습니다 (예 : [here] (http://msdn.microsoft.com/en-us/library/swta9c6e%28VS.80%29.aspx)).하지만 boost :: function '나는 실제로 메소드가 아니라 멤버 ** 객체 **를 가지고 있습니다. 그래서 그것은 templatized 수 없습니다. 이 올바른지? –

+0

@MatteoM., "class method"용어가 C++에는 존재하지 않습니다. 대신 "멤버 함수"에 익숙해 져야합니다. 하지만 그래, 개념은 정확합니다. 'boost :: function'은 클래스 형식입니다. boost :: function test_fun; 선언은 템플릿화할 수없는 객체입니다. – Shoe