c++ - BGL : specialization of template in different namespace

标签 c++ boost-graph

我正在通过 BGL 创建一个图,并且想将捆绑属性与 vertex_index_t 结合起来,因为图的 VertexPropertylistS

我使用了 BGL dijkstra_shortest_path algorithm method does not accept my color map exterior property 中的方法,但是,我最终遇到了 specialization of template in different namespace 错误。

#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;
};

}

我尝试了以下代码,但没有用。

namespace MyNameSpace {

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

}

}

请帮帮我。

编辑

下面是我目前的解决方案,不知道对不对。

#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
}

最佳答案

模板显式特化应该在定义模板的命名空间范围内。

由于您发布的代码不是最小示例,这里是重现问题的最小示例。

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

参见 Demo. .

要编译上面的示例,您可以将特化移出 namespace B 或者您甚至可以将它移出 namespace A,前提是您在专门化它时使用嵌套名称说明符。

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

参见 Demo.

关于c++ - BGL : specialization of template in different namespace,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55704527/

相关文章:

c++ - 具有优先队列的 BGL DFS 访问者

c++ - 为什么 std::sort() 会改变排序后的 vector ?

c++ - 将 += 与字符串一起使用时出现段错误

c++ - 具有时间相关图的 Astar

c++ - 添加外部属性以包含图中顶点的索引( boost )

c++ - BGL : Unable to access bundled vertex properties

c++ - 使用图权重 boost 深度优先访问者最小生成树

c++ - 如何重写像vector这样的c++类

c++ - 使用备用编译器构建 Travis-CI R 项目

c++ - 在函数上获取段错误