Fixes in the map concepts
authorPeter Kovacs <kpeter@inf.elte.hu>
Tue, 18 Mar 2008 13:57:15 +0100
changeset 94a4688e4138ec
parent 93 f857981306ea
child 95 cc7e6b8b59bf
Fixes in the map concepts

- Now Value type needn't be default constructible.
- Extend the test file to check this.
lemon/concepts/maps.h
test/maps_test.cc
     1.1 --- a/lemon/concepts/maps.h	Mon Mar 17 19:21:27 2008 +0000
     1.2 +++ b/lemon/concepts/maps.h	Tue Mar 18 13:57:15 2008 +0100
     1.3 @@ -47,10 +47,9 @@
     1.4        typedef T Value;
     1.5  
     1.6        /// Returns the value associated with the given key.
     1.7 -
     1.8 -      /// Returns the value associated with the given key.
     1.9 -      /// \bug Value shouldn't need to be default constructible. 
    1.10 -      Value operator[](const Key &) const { return Value(); }
    1.11 +      Value operator[](const Key &) const { 
    1.12 +        return *static_cast<Value *>(0);
    1.13 +      }
    1.14  
    1.15        template<typename _ReadMap>
    1.16        struct Constraints {
    1.17 @@ -126,7 +125,9 @@
    1.18        typedef T Value;
    1.19  
    1.20        /// Returns the value associated with the given key.
    1.21 -      Value operator[](const Key &) const { return Value(); }
    1.22 +      Value operator[](const Key &) const { 
    1.23 +        return *static_cast<Value *>(0);
    1.24 +      }
    1.25  
    1.26        /// Sets the value associated with the given key.
    1.27        void set(const Key &, const Value &) {}
    1.28 @@ -160,15 +161,17 @@
    1.29        /// The const reference type of the map.
    1.30        typedef CR ConstReference;
    1.31  
    1.32 -    protected:
    1.33 -      Value tmp;
    1.34      public:
    1.35  
    1.36        /// Returns a reference to the value associated with the given key.
    1.37 -      Reference operator[](const Key &) { return tmp; }
    1.38 +      Reference operator[](const Key &) { 
    1.39 +        return *static_cast<Value *>(0);
    1.40 +      }
    1.41  
    1.42        /// Returns a const reference to the value associated with the given key.
    1.43 -      ConstReference operator[](const Key &) const { return tmp; }
    1.44 +      ConstReference operator[](const Key &) const {
    1.45 +        return *static_cast<Value *>(0);
    1.46 +      }
    1.47  
    1.48        /// Sets the value associated with the given key.
    1.49        void set(const Key &k,const Value &t) { operator[](k)=t; }
     2.1 --- a/test/maps_test.cc	Mon Mar 17 19:21:27 2008 +0000
     2.2 +++ b/test/maps_test.cc	Tue Mar 18 13:57:15 2008 +0100
     2.3 @@ -32,6 +32,12 @@
     2.4  inline bool operator<(A, A) { return true; }
     2.5  struct B {};
     2.6  
     2.7 +class C {
     2.8 +  int x;
     2.9 +public:
    2.10 +  C(int _x) : x(_x) {}
    2.11 +};
    2.12 +
    2.13  class F {
    2.14  public:
    2.15    typedef A argument_type;
    2.16 @@ -58,9 +64,13 @@
    2.17  {
    2.18    // Map concepts
    2.19    checkConcept<ReadMap<A,B>, ReadMap<A,B> >();
    2.20 +  checkConcept<ReadMap<A,C>, ReadMap<A,C> >();
    2.21    checkConcept<WriteMap<A,B>, WriteMap<A,B> >();
    2.22 +  checkConcept<WriteMap<A,C>, WriteMap<A,C> >();
    2.23    checkConcept<ReadWriteMap<A,B>, ReadWriteMap<A,B> >();
    2.24 +  checkConcept<ReadWriteMap<A,C>, ReadWriteMap<A,C> >();
    2.25    checkConcept<ReferenceMap<A,B,B&,const B&>, ReferenceMap<A,B,B&,const B&> >();
    2.26 +  checkConcept<ReferenceMap<A,C,C&,const C&>, ReferenceMap<A,C,C&,const C&> >();
    2.27  
    2.28    // NullMap
    2.29    {