2

I have read the boost docs to figure out how to use property_map.

Based on

// Property map accessors
template<typename PropertyTag>
property_map<compressed_sparse_row_graph, PropertyTag>::type
get(PropertyTag, compressed_sparse_row_graph& g)

I wrote the following code:

#include <boost/graph/graph_traits.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/compressed_sparse_row_graph.hpp>
#include <boost/utility.hpp>

typedef boost::compressed_sparse_row_graph<boost::bidirectionalS, boost::no_property, boost::property<boost::edge_weight_t, int> > AdjGraph;
typedef typename boost::property_map<AdjGraph, boost::edge_weight_t>::type WeightMap;
class data {
    WeightMap weight;
    data()
    {
        std::vector<std::pair<int, int> > edges;
        std::vector<int> edgesAttr;
        boost::shared_ptr<AdjGraph> adjGraph;
        adjGraph = boost::shared_ptr<AdjGraph>(new AdjGraph(boost::edges_are_unsorted_multi_pass, edges.begin(), edges.end(), edgesAttr.begin(), 0));
        weight = boost::get(boost::edge_weight, *adjGraph);
    }
};

int main() { return 0; }

But errors were reported when I tried to compiled it.

I modified

weight = boost::get(boost::edge_weight, *adjGraph);

to be

auto tmp = boost::get(boost::edge_weight, *adjGraph);

And it compiles well.

But as "weight" should not be a static variable, "auto weight" is unacceptable.

I want to know what type "weight" should be. I tried "typeinfo" and "typeid().name()" but the output is unreadable.

Though I refer to 1.61 docs, I am actually using 1.58 1.58 docs

Zehui Lin
  • 186
  • 9
  • What are you trying to do? Associate a property map with a graph? Does this help? http://stackoverflow.com/questions/12501188/iterating-through-edge-weights-of-a-const-boostgraph – doctorlove Dec 13 '16 at 14:55
  • @doctorlove that's not it – sehe Dec 13 '16 at 15:15
  • 1
    Another trick: use `struct {} _ = expression;` and the compiler error message will detail exactly the type of ` that expression. Note however, that only the documentation will give you the portable way to formulate that type as you already had it (WeightMap) – sehe Dec 13 '16 at 15:28

2 Answers2

3

I want to know what type "weight" should be

The type is WeightMap. You already have it correct. You're solving the wrong problem. This simply compiles

WeightMap weight = boost::get(boost::edge_weight, *adjGraph);

Then what is the problem?

WeightMap is not default-constructible. Like all property-maps it's just a lightweight, low-cost-copyable "reference" to the actual data (in this case inside the graph model).

Therefore, there is zero reason to store it in a member, or share it to the outside world.

On a more essential level, because property-maps are usually (and certainly in this case) references to an underlying object, its lifetime is only valid as long as the underlying graph is.

Therefore it doesn't make sense to keep the weight map in a member unless you also keep a shared pointer to the graph in an earlier member:

Live On Wandbox

#include <boost/graph/graph_traits.hpp>
#include <boost/graph/compressed_sparse_row_graph.hpp>
#include <boost/utility.hpp>

typedef boost::compressed_sparse_row_graph<boost::bidirectionalS, boost::no_property, boost::property<boost::edge_weight_t, int> > AdjGraph;
typedef typename boost::property_map<AdjGraph, boost::edge_weight_t>::type WeightMap;

class data {
    boost::shared_ptr<AdjGraph> adjGraph;
    WeightMap weight;
  public:
    data(std::vector<std::pair<int, int> > const& edges, std::vector<int> const& edgesAttr) 
        : adjGraph (boost::shared_ptr<AdjGraph>(new AdjGraph(boost::edges_are_unsorted_multi_pass, edges.begin(), edges.end(), edgesAttr.begin(), 0))),
          weight(boost::get(boost::edge_weight, *adjGraph))
    {
    }
};

int main() {
    std::vector<std::pair<int, int> > edges;
    std::vector<int> edgesAttr;

    data d(edges, edgesAttr);
}
sehe
  • 374,641
  • 47
  • 450
  • 633
  • Though I refer to 1.61 docs in the problem description, I am actually using 1.58. In [boost 1.58 doc](http://www.boost.org/doc/libs/1_58_0/libs/graph/doc/compressed_sparse_row.html), the description of function get is the same as 1.61. The problem is caused be bugs of boost library. Thank you for your help! – Zehui Lin Dec 13 '16 at 15:21
  • Please post an answer where you describe the problem and the solution. My answer clearly explains the problem _in your sample_ and shows how to fix it (this makes sense, we can't answer questions that have not been asked) – sehe Dec 13 '16 at 15:27
  • As I am new to stackoverflow, I don't know much about what I should do. You help me a lot. Thank you for your advice. – Zehui Lin Dec 14 '16 at 17:26
1

After fixing the problem of initializing weight, warnings would still be reported if compiling with boost 1.58 and -std=gnu++11: wandbox

In file included from /usr/local/boost-1.59.0/include/boost/smart_ptr/shared_ptr.hpp:28:0,
                 from /usr/local/boost-1.59.0/include/boost/shared_ptr.hpp:17,
                 from /usr/local/boost-1.59.0/include/boost/property_map/vector_property_map.hpp:14,
                 from /usr/local/boost-1.59.0/include/boost/property_map/property_map.hpp:600,
                 from /usr/local/boost-1.59.0/include/boost/graph/properties.hpp:19,
                 from /usr/local/boost-1.59.0/include/boost/graph/compressed_sparse_row_graph.hpp:26,
                 from prog.cc:2:
/usr/local/boost-1.59.0/include/boost/smart_ptr/detail/shared_count.hpp:396:33: warning: 'template<class> class std::auto_ptr' is deprecated [-Wdeprecated-declarations]
     explicit shared_count( std::auto_ptr<Y> & r ): pi_( new sp_counted_impl_p<Y>( r.get() ) )
                                 ^~~~~~~~
In file included from /usr/local/gcc-head/include/c++/7.0.0/memory:80:0,
                 from /usr/local/boost-1.59.0/include/boost/config/no_tr1/memory.hpp:21,
                 from /usr/local/boost-1.59.0/include/boost/smart_ptr/shared_ptr.hpp:23,
                 from /usr/local/boost-1.59.0/include/boost/shared_ptr.hpp:17,
                 from /usr/local/boost-1.59.0/include/boost/property_map/vector_property_map.hpp:14,
                 from /usr/local/boost-1.59.0/include/boost/property_map/property_map.hpp:600,
                 from /usr/local/boost-1.59.0/include/boost/graph/properties.hpp:19,
                 from /usr/local/boost-1.59.0/include/boost/graph/compressed_sparse_row_graph.hpp:26,
                 from prog.cc:2:
/usr/local/gcc-head/include/c++/7.0.0/bits/unique_ptr.h:51:28: note: declared here
   template<typename> class auto_ptr;
                            ^~~~~~~~
In file included from /usr/local/boost-1.59.0/include/boost/shared_ptr.hpp:17:0,
                 from /usr/local/boost-1.59.0/include/boost/property_map/vector_property_map.hpp:14,
                 from /usr/local/boost-1.59.0/include/boost/property_map/property_map.hpp:600,
                 from /usr/local/boost-1.59.0/include/boost/graph/properties.hpp:19,
                 from /usr/local/boost-1.59.0/include/boost/graph/compressed_sparse_row_graph.hpp:26,
                 from prog.cc:2:
/usr/local/boost-1.59.0/include/boost/smart_ptr/shared_ptr.hpp:249:65: warning: 'template<class> class std::auto_ptr' is deprecated [-Wdeprecated-declarations]
 template< class T, class R > struct sp_enable_if_auto_ptr< std::auto_ptr< T >, R >
                                                                 ^~~~~~~~
In file included from /usr/local/gcc-head/include/c++/7.0.0/memory:80:0,
                 from /usr/local/boost-1.59.0/include/boost/config/no_tr1/memory.hpp:21,
                 from /usr/local/boost-1.59.0/include/boost/smart_ptr/shared_ptr.hpp:23,
                 from /usr/local/boost-1.59.0/include/boost/shared_ptr.hpp:17,
                 from /usr/local/boost-1.59.0/include/boost/property_map/vector_property_map.hpp:14,
                 from /usr/local/boost-1.59.0/include/boost/property_map/property_map.hpp:600,
                 from /usr/local/boost-1.59.0/include/boost/graph/properties.hpp:19,
                 from /usr/local/boost-1.59.0/include/boost/graph/compressed_sparse_row_graph.hpp:26,
                 from prog.cc:2:
/usr/local/gcc-head/include/c++/7.0.0/bits/unique_ptr.h:51:28: note: declared here
   template<typename> class auto_ptr;
                            ^~~~~~~~
In file included from /usr/local/boost-1.59.0/include/boost/shared_ptr.hpp:17:0,
                 from /usr/local/boost-1.59.0/include/boost/property_map/vector_property_map.hpp:14,
                 from /usr/local/boost-1.59.0/include/boost/property_map/property_map.hpp:600,
                 from /usr/local/boost-1.59.0/include/boost/graph/properties.hpp:19,
                 from /usr/local/boost-1.59.0/include/boost/graph/compressed_sparse_row_graph.hpp:26,
                 from prog.cc:2:
/usr/local/boost-1.59.0/include/boost/smart_ptr/shared_ptr.hpp:448:31: warning: 'template<class> class std::auto_ptr' is deprecated [-Wdeprecated-declarations]
     explicit shared_ptr( std::auto_ptr<Y> & r ): px(r.get()), pn()
                               ^~~~~~~~
In file included from /usr/local/gcc-head/include/c++/7.0.0/memory:80:0,
                 from /usr/local/boost-1.59.0/include/boost/config/no_tr1/memory.hpp:21,
                 from /usr/local/boost-1.59.0/include/boost/smart_ptr/shared_ptr.hpp:23,
                 from /usr/local/boost-1.59.0/include/boost/shared_ptr.hpp:17,
                 from /usr/local/boost-1.59.0/include/boost/property_map/vector_property_map.hpp:14,
                 from /usr/local/boost-1.59.0/include/boost/property_map/property_map.hpp:600,
                 from /usr/local/boost-1.59.0/include/boost/graph/properties.hpp:19,
                 from /usr/local/boost-1.59.0/include/boost/graph/compressed_sparse_row_graph.hpp:26,
                 from prog.cc:2:
/usr/local/gcc-head/include/c++/7.0.0/bits/unique_ptr.h:51:28: note: declared here
   template<typename> class auto_ptr;
                            ^~~~~~~~
In file included from /usr/local/boost-1.59.0/include/boost/shared_ptr.hpp:17:0,
                 from /usr/local/boost-1.59.0/include/boost/property_map/vector_property_map.hpp:14,
                 from /usr/local/boost-1.59.0/include/boost/property_map/property_map.hpp:600,
                 from /usr/local/boost-1.59.0/include/boost/graph/properties.hpp:19,
                 from /usr/local/boost-1.59.0/include/boost/graph/compressed_sparse_row_graph.hpp:26,
                 from prog.cc:2:
/usr/local/boost-1.59.0/include/boost/smart_ptr/shared_ptr.hpp:461:22: warning: 'template<class> class std::auto_ptr' is deprecated [-Wdeprecated-declarations]
     shared_ptr( std::auto_ptr<Y> && r ): px(r.get()), pn()
                      ^~~~~~~~
In file included from /usr/local/gcc-head/include/c++/7.0.0/memory:80:0,
                 from /usr/local/boost-1.59.0/include/boost/config/no_tr1/memory.hpp:21,
                 from /usr/local/boost-1.59.0/include/boost/smart_ptr/shared_ptr.hpp:23,
                 from /usr/local/boost-1.59.0/include/boost/shared_ptr.hpp:17,
                 from /usr/local/boost-1.59.0/include/boost/property_map/vector_property_map.hpp:14,
                 from /usr/local/boost-1.59.0/include/boost/property_map/property_map.hpp:600,
                 from /usr/local/boost-1.59.0/include/boost/graph/properties.hpp:19,
                 from /usr/local/boost-1.59.0/include/boost/graph/compressed_sparse_row_graph.hpp:26,
                 from prog.cc:2:
/usr/local/gcc-head/include/c++/7.0.0/bits/unique_ptr.h:51:28: note: declared here
   template<typename> class auto_ptr;
                            ^~~~~~~~
In file included from /usr/local/boost-1.59.0/include/boost/shared_ptr.hpp:17:0,
                 from /usr/local/boost-1.59.0/include/boost/property_map/vector_property_map.hpp:14,
                 from /usr/local/boost-1.59.0/include/boost/property_map/property_map.hpp:600,
                 from /usr/local/boost-1.59.0/include/boost/graph/properties.hpp:19,
                 from /usr/local/boost-1.59.0/include/boost/graph/compressed_sparse_row_graph.hpp:26,
                 from prog.cc:2:
/usr/local/boost-1.59.0/include/boost/smart_ptr/shared_ptr.hpp:538:34: warning: 'template<class> class std::auto_ptr' is deprecated [-Wdeprecated-declarations]
     shared_ptr & operator=( std::auto_ptr<Y> & r )
                                  ^~~~~~~~
In file included from /usr/local/gcc-head/include/c++/7.0.0/memory:80:0,
                 from /usr/local/boost-1.59.0/include/boost/config/no_tr1/memory.hpp:21,
                 from /usr/local/boost-1.59.0/include/boost/smart_ptr/shared_ptr.hpp:23,
                 from /usr/local/boost-1.59.0/include/boost/shared_ptr.hpp:17,
                 from /usr/local/boost-1.59.0/include/boost/property_map/vector_property_map.hpp:14,
                 from /usr/local/boost-1.59.0/include/boost/property_map/property_map.hpp:600,
                 from /usr/local/boost-1.59.0/include/boost/graph/properties.hpp:19,
                 from /usr/local/boost-1.59.0/include/boost/graph/compressed_sparse_row_graph.hpp:26,
                 from prog.cc:2:
/usr/local/gcc-head/include/c++/7.0.0/bits/unique_ptr.h:51:28: note: declared here
   template<typename> class auto_ptr;
                            ^~~~~~~~
In file included from /usr/local/boost-1.59.0/include/boost/shared_ptr.hpp:17:0,
                 from /usr/local/boost-1.59.0/include/boost/property_map/vector_property_map.hpp:14,
                 from /usr/local/boost-1.59.0/include/boost/property_map/property_map.hpp:600,
                 from /usr/local/boost-1.59.0/include/boost/graph/properties.hpp:19,
                 from /usr/local/boost-1.59.0/include/boost/graph/compressed_sparse_row_graph.hpp:26,
                 from prog.cc:2:
/usr/local/boost-1.59.0/include/boost/smart_ptr/shared_ptr.hpp:547:34: warning: 'template<class> class std::auto_ptr' is deprecated [-Wdeprecated-declarations]
     shared_ptr & operator=( std::auto_ptr<Y> && r )
                                  ^~~~~~~~
In file included from /usr/local/gcc-head/include/c++/7.0.0/memory:80:0,
                 from /usr/local/boost-1.59.0/include/boost/config/no_tr1/memory.hpp:21,
                 from /usr/local/boost-1.59.0/include/boost/smart_ptr/shared_ptr.hpp:23,
                 from /usr/local/boost-1.59.0/include/boost/shared_ptr.hpp:17,
                 from /usr/local/boost-1.59.0/include/boost/property_map/vector_property_map.hpp:14,
                 from /usr/local/boost-1.59.0/include/boost/property_map/property_map.hpp:600,
                 from /usr/local/boost-1.59.0/include/boost/graph/properties.hpp:19,
                 from /usr/local/boost-1.59.0/include/boost/graph/compressed_sparse_row_graph.hpp:26,
                 from prog.cc:2:
/usr/local/gcc-head/include/c++/7.0.0/bits/unique_ptr.h:51:28: note: declared here
   template<typename> class auto_ptr;
                            ^~~~~~~~
In file included from /usr/local/boost-1.59.0/include/boost/shared_ptr.hpp:17:0,
                 from /usr/local/boost-1.59.0/include/boost/property_map/vector_property_map.hpp:14,
                 from /usr/local/boost-1.59.0/include/boost/property_map/property_map.hpp:600,
                 from /usr/local/boost-1.59.0/include/boost/graph/properties.hpp:19,
                 from /usr/local/boost-1.59.0/include/boost/graph/compressed_sparse_row_graph.hpp:26,
                 from prog.cc:2:
/usr/local/boost-1.59.0/include/boost/smart_ptr/shared_ptr.hpp: In member function 'boost::shared_ptr<T>& boost::shared_ptr<T>::operator=(std::auto_ptr<_Up>&&)':
/usr/local/boost-1.59.0/include/boost/smart_ptr/shared_ptr.hpp:549:38: warning: 'template<class> class std::auto_ptr' is deprecated [-Wdeprecated-declarations]
         this_type( static_cast< std::auto_ptr<Y> && >( r ) ).swap( *this );
                                      ^~~~~~~~
In file included from /usr/local/gcc-head/include/c++/7.0.0/memory:80:0,
                 from /usr/local/boost-1.59.0/include/boost/config/no_tr1/memory.hpp:21,
                 from /usr/local/boost-1.59.0/include/boost/smart_ptr/shared_ptr.hpp:23,
                 from /usr/local/boost-1.59.0/include/boost/shared_ptr.hpp:17,
                 from /usr/local/boost-1.59.0/include/boost/property_map/vector_property_map.hpp:14,
                 from /usr/local/boost-1.59.0/include/boost/property_map/property_map.hpp:600,
                 from /usr/local/boost-1.59.0/include/boost/graph/properties.hpp:19,
                 from /usr/local/boost-1.59.0/include/boost/graph/compressed_sparse_row_graph.hpp:26,
                 from prog.cc:2:
/usr/local/gcc-head/include/c++/7.0.0/bits/unique_ptr.h:51:28: note: declared here
   template<typename> class auto_ptr;
                            ^~~~~~~~

The reason is deprecation of auto_ptr

To fix this, use boost 1.60 or later version, or compile with -std=gnu++98.

Zehui Lin
  • 186
  • 9
  • 1
    The initializing of `weight` is the only problem present in your question. Posting "other problems are caused" isn't helping anyone find a solution in case he/she runs into the same problem. You need to actually fix the question for the answer to be useful. – sehe Dec 13 '16 at 21:49
  • 1
    Thanks for the edit. For people to find this, you'd need to describe the error, not the thing that should work. What does the compiler say? What are the key ingredients for it to fail? – sehe Dec 14 '16 at 10:39
  • 1
    Wonderful. That's way more helpful. Now that you actually included the compiler messages, we can conclude it **wasn't** an error, but a warning. A deprecation of `auto_ptr`. That's very googlable. (The best solution is upgrading boost) – sehe Dec 14 '16 at 19:57