Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Problem with function template parameter and std::pair

This thread is locked; no one can reply to it. rss feed Print
Problem with function template parameter and std::pair
Martin KalbfuƟ
Member #9,131
October 2007
avatar

I try to pass a function type as template parameter. It's the first time I do this, and I'm not really surprised that it isn't working.

#SelectExpand
1#pragma once 2 3#include <map> 4 5template <typename T, typename Dtor=void(T*)> 6class ResourceManager 7{ 8 typedef typename std::map<T*, Dtor> Resources; 9 typedef typename Resources::iterator Iterator; 10 11private: 12 Resources resources; 13 14public: 15 ResourceManager(); 16 ~ResourceManager(); 17 void add_resource(T *resource, Dtor dtor); 18 void del_resource(T *resource); 19}; 20 21template <typename T, typename Dtor> 22ResourceManager<T, Dtor>::ResourceManager() {}; 23 24template <typename T, typename Dtor> 25void ResourceManager<T, Dtor>::add_resource(T *resource, Dtor dtor) 26{ 27 resources[resource] = dtor; 28} 29 30template <typename T, typename Dtor> 31void ResourceManager<T, Dtor>::del_resource(T *resource) 32{ 33 Iterator it = resources.find(resource); 34 if(it != resources.end()) 35 { 36 (*it).second(resource); 37 resources.erase(it); 38 } 39} 40 41template <typename T, typename Dtor> 42ResourceManager<T, Dtor>::~ResourceManager() 43{ 44 for(Iterator it = resources.begin(); it != resources.end(); it++) 45 (*it).second((*it).first); 46 47 resources.clear(); 48}

error: field 'std::pair<_SDL_TimerID* const, SDL_bool ()(_SDL_TimerID*)>::second' invalidly declared function type

What's the right way to do it?

Thanks.

Martin

Edit:

Oh no. :-X. I missed the (*). Problem solved. But with boost::function it is any_type(any_type). How do they do this :o ?

http://remote-lisp.spdns.de -- my server side lisp interpreter
http://www.nongnu.org/gm2/ -- Modula-2 alias Pascal++

axilmar
Member #1,204
April 2001

Quote:

How do they do this :o ?

Example:

#SelectExpand
1//template prototype 2template <class T> class Foo { 3}; 4 5//specialization for function with 0 params 6template <class R> class Foo<R()> { 7}; 8 9//specialization for function with 1 param 10template <class R, class T1> class Foo<R(T1)> { 11}; 12 13//specialization for function with 2 params 14template <class R, class T1, class T2> class Foo<R(T1, T2)> { 15};

anonymous
Member #8025
November 2006

In this case that would amount to:

namespace std {
template <class T, class U>
struct pair<T, void(U*)>
{
    T first;
    void (*second)(U*);
};
}

Would not recommend, even if adding the specialization is legal according to the standard (?).

Go to: