std::function
클래스 템플릿 내에서 펑터를 객체 로 사용하려고했습니다 . 아래는 내가 지금까지 한 일입니다.
//! the functor class template
template<typename T>
struct func
{
void operator ()(T t)
{
std::cout << t << "\n";
}
};
//! the class template that holds a std::function object as a member
template<typename T>
struct Foo
{
std::function<void(T)> bar = func<T>();
};
int main()
{
Foo<int> foo;
return 0;
}
불평했다
error: conversion from 'func<int>' to non-scalar type 'std::function<void(int)>' requested
struct Foo
^
그렇게 할 수 있습니까? 그것을 고치는 방법?
정적으로 만들고 클래스 범위 외부에서 초기화하거나 생성자에서 초기화 할 수 있습니다. GCC 4.7.2에서 테스트되었습니다.
template<typename T>
struct Foo
{
static std::function<void(T)> bar;
};
template <typename T>
std::function<void(T)> Foo<T>::bar = func<T>();
편집하다
C ++ 11에서는 중괄호 초기화를 사용할 수도 있습니다.
std::function<void(T)> bar { func<T>() };
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다