User1291
User1291

Reputation: 8189

C++ typedefs requiring ::type

Trying to start with BGL, which means I'm starting with a lot of typedefs:

#include <iostream> //std::cin, std::cout
#include <tuple> //std::tie

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

using namespace boost;
typedef adjacency_list<vecS,vecS,undirectedS,no_property,property<edge_weight_t,int> > Graph;
typedef graph_traits<Graph> Traits;
typedef Traits::vertex_descriptor Vertex;
typedef Traits::edge_descriptor Edge;
typedef property_map<Graph, edge_weight_t>::type EdgeWeightMap; //::type necessary (why?)

int main(int argc, char* argv[]){
    int n = ...;
    Graph g(n);
    EdgeWeightMap weight_of;
    Edge e;
    bool success;
    int s,t,w;
    std::cin >> s >> t >> w;
    tie(e,success) = add_edge(s,t,g);
    if(success)weight_of[e] = w;
}

And I was wondering just why the ::type in the typedef for EdgeWeightMap was necessary. If I omit it, I get

error: no match for ‘operator[]’ (operand types are

‘EdgeWeightMap {aka boost::property_map<boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, boost::no_property, boost::property<boost::edge_weight_t, int> >, boost::edge_weight_t>}’

and

‘Edge {aka boost::detail::edge_desc_impl<boost::undirected_tag, unsigned int>}’

) weight_of[e] = w;

(Sorry about the format, the typedefs' < and > seem to be interfering with the blockquote)

And indeed, when I try

EdgeWeightMap weight_of = get(edge_weight,g);

I get an

error: conversion from

‘boost::detail::adj_list_any_edge_pmap::bind_<boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, boost::no_property, boost::property<boost::edge_weight_t, int> >, boost::property<boost::edge_weight_t, int>, boost::edge_weight_t>::type {aka boost::adj_list_edge_property_map<boost::undirected_tag, int, int&, unsigned int, boost::property<boost::edge_weight_t, int>, boost::edge_weight_t>}’

to non-scalar type

‘EdgeWeightMap {aka boost::property_map<boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, boost::no_property, boost::property<boost::edge_weight_t, int> >, boost::edge_weight_t>}’

requested EdgeWeightMap weight_of = get(edge_weight,g);

Now, I can see these are different types, what I don't get is why they differ. And as I kind of would prefer to avoid surprises, could somebody please enlighten me as to when the ::type is required and when it must not be used?

Upvotes: 0

Views: 102

Answers (1)

Daniel
Daniel

Reputation: 4549

From reading the property_map documentation, the "property_map" template is designed to provide types, not be a type. The property_map structure contains exactly two types. type and const_type, for mutable and immutable types respectively.

Upvotes: 1

Related Questions