0

I am creating a graph by BGL, and would like to combine bundled properties with vertex_index_t as the VertexProperty of the graph is listS.

I used the method from BGL dijkstra_shortest_path algorithm method does not accept my color map exterior property , however, I end up with specialization of template in different namespace error.

#include <boost/graph/adjacency_list.hpp>

namespace MyNameSpace {

using namespace boost;

struct VertexP {
    std::string name;
    unsigned int id;
};

typedef adjacency_list<vecS, listS, bidirectionalS, VertexP> Graph;

class VertexIndexMap {
public:
    typedef boost::readable_property_map_tag category;
    typedef size_t  value_type;
    typedef value_type reference;
    typedef Graph::vertex_descriptor key_type;

    VertexIndexMap(const Graph& g): _g(&g) {}

    const Graph * _g;
};

template<>
struct property_map<Graph, vertex_index_t > {
    typedef VertexIndexMap const_type;
};

}

I tried the following code, but not work.

namespace MyNameSpace {

namespace boost {
template<>
struct property_map<Graph, vertex_index_t > {
    typedef VertexIndexMap const_type;
};

}

}

Please help me out.

EDIT

Below is my current solution, don't know if it's correct.

#include <boost/graph/adjacency_list.hpp>

namespace MyNameSpace {

using namespace boost;

struct VertexP {
    std::string name;
    unsigned int id;
};

typedef adjacency_list<vecS, listS, bidirectionalS, VertexP> Graph;

class VertexIndexMap {
public:
    typedef boost::readable_property_map_tag category;
    typedef size_t  value_type;
    typedef value_type reference;
    typedef Graph::vertex_descriptor key_type;

    VertexIndexMap(const Graph& g): _g(&g) {}

    const Graph * _g;
};

}

namespace boost {

template<>
struct property_map<Graph, vertex_index_t > {
    typedef VertexIndexMap const_type;
};

}

namespace MyNameSpace {
  // the remaining code in namespace MyNameSpace
}
chenwj
  • 1,989
  • 2
  • 16
  • 30
  • 2
    What's unclear about the error message? You can't specialize a template changing the namespaces used for the declaration of that template type. – πάντα ῥεῖ Apr 16 '19 at 09:11

1 Answers1

1

A template explicit specialization should be in the scope of the namespace in which the template was defined.

Since the code you have posted is not a minimal example, here is a minimal example to reproduce the problem.

namespace A {
    template<class T> class X { /* ... */ };
    namespace B {
        template<> class X<int> { /* ... */ };
    }
}

See Demo..

To make the above example compile you can move the specialization out of namespace B or you can even move it out of namespace A provided you use a nested name specifier when you specialize it.

template<> class A::X<int> { /* ... */ };

See Demo.

P.W
  • 26,289
  • 6
  • 39
  • 76
  • I updated the code example, g++-7 will produce the same error. The problem is the type parameter declared in namespace A (or MyNameSpce in my example). – chenwj Apr 17 '19 at 02:32
  • The compiler version does not matter. This is not allowed. – P.W Apr 17 '19 at 04:22
  • I add my current solution in my original post, could you review and comment it? Thanks. – chenwj Apr 17 '19 at 05:12