开发者

C++: using function pointers as template arguments

I have the following code:

template<typename Parent, typename T, void (Parent::*Setter)(T), T (Parent::*Getter)()>
struct Property {
 Parent& obj;
 Property(Par开发者_如何学Goent& _obj) : obj(_obj) {}
 Property& operator=(T v) { (obj.*Setter)(v); return *this; }
 operator T() { return (obj.*Getter)(); }
};

template<typename T1, typename T2>
class Bimap {
public:
 class Entry;
 typedef std::tr1::shared_ptr<Entry> EntryP; 
 typedef std::multimap<T1, EntryP> T1Map;

 class Entry {
 private:
  Bimap& bimap;
  typename T1Map::iterator it1;

  void set1(T1 v) { bimap.map1.erase(it1); it1 = bimap.map1.insert(typename T1Map::value_type(v, it2->second)).first; }
  T1 get1() { return it1->first; }
 public:
  Property<Entry,T1,set1,get1> first() { return Property<Entry,T1,set1,get1>(*this); }
 };
};

This code gives me (on Property<...> first()):

'void Bimap<T1, T2>::Entry::set1(T1) [with T1 = double, T2 = std::tr1::shared_ptr<Node>]' cannot appear in a constant-expression

I also tried with

Property<Entry,T1,&set1,&get1> first() { return Property<Entry,T1,&set1,&get1>(*this); }

but that gives me:

error: wrong number of template arguments (3, should be 4)

error: provided for 'template<class Parent, class T, void (Parent::* Setter)(T), T (Parent::* Getter)()> struct Property'

Is it possible to fix this somehow? Or why is that not possible?


You want

Property<Entry, T1, &Entry::set1, &Entry::get1>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜