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
  • These aren't ordinary function pointers, these are pointer-to-member functions, so you have to use the pointer-to-member syntax. Commented Sep 19, 2010 at 17:50

1 Answer 1

2

You want

Property<Entry, T1, &Entry::set1, &Entry::get1>
Sign up to request clarification or add additional context in comments.

2 Comments

Do you think the error message from the compiler can be improved? Just curious
@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.