Reputation: 306
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
Upvotes: 2
Views: 199
Reputation: 306
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.
Upvotes: 1
Reputation: 392911
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:
#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);
}
Upvotes: 3