I am trying to attach an object object that needs to be called when the template class is destroyed. However, I don't seem to be able to pass the object to the function as temporary. The warning I get (if a line comment xi.data = 5;):
warning C4930: 'X<T> xi2(writer (__cdecl *)(void))':
prototyped function not called (was a variable definition intended?)
with
[
T=int
]
and if I try to use the constructed object, I get a compilation error:
error C2228: left of '.data' must have class/struct/union
I apologize for the long piece of code, but I think that all components should be visible to assess the situation.
template<typename T>
struct Base
{
virtual void run( T& ){}
virtual ~Base(){}
};
template<typename T, typename D>
struct Derived : public Base<T>
{
virtual void run( T& t )
{
D d;
d(t);
}
};
template<typename T>
struct X
{
template<typename R>
X(const R& r)
{
std::cout << "X(R)" << std::endl;
ptr = new Derived<T,R>();
}
X():ptr(0)
{
std::cout << "X()" << std::endl;
}
~X()
{
if(ptr)
{
ptr->run(data);
delete ptr;
}
else
{
std::cout << "no ptr" << std::endl;
}
}
Base<T>* ptr;
T data;
};
struct writer
{
template<typename T>
void operator()( const T& i )
{
std::cout << "T : " << i << std::endl;
}
};
int main()
{
{
writer w;
X<int> xi2(w);
xi2.data = 15;
}
return 0;
};
, , , "- " - , - . , class X, class writer, Base<T> ( , <T> , ).
, , , writer, X, X<int> xi(writer();
- , ?