tags:

views:

72

answers:

1

I have the following code:

template<typename Parent, typename T, void (Parent::*Setter)(T), T (Parent::*Getter)()>
struct Property {
 Parent& obj;
 Property(Parent& _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?

+1  A: 

You want

Property<Entry, T1, &Entry::set1, &Entry::get1>
Johannes Schaub - litb
Do you think the error message from the compiler can be improved? Just curious
Chubsdad
@Chubsdad I'm quite sure that it can. An implementation knows that `set1` refers to a nonstatic member function, and knows that it tries to initialize a pointer to member. I suspect that GCC just doesn't care.
Johannes Schaub - litb