我正在通过 BGL 创建一个图,并且想将捆绑属性与 vertex_index_t
结合起来,因为图的 VertexProperty
是 listS
。
我使用了 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/