BGL:不同命名空间中模板的专用化
BGL: specialization of template in different namespace
我正在通过BGL创建一个图,并希望将捆绑的属性与vertex_index_t
结合起来,因为图的VertexProperty
是listS
。
我使用了BGL的方法dijkstra_shortest_path算法方法不接受我的色图外观属性,但是,我最终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> { /* ... */ };
}
}
请参阅演示..
要使上面的示例编译,您可以将专用化移出namespace B
或者,您甚至可以将其移出namespace A
前提是您在专用化它时使用嵌套名称说明符。
template<> class A::X<int> { /* ... */ };
请参阅演示。
相关文章:
- 命名空间中具有.h和.cpp文件的类
- 从父命名空间重载类型
- 在命名空间中定义函数还是限定函数
- C++:对不存在的命名空间使用命名空间指令
- 通过继承类使用来自不同命名空间的运算符
- 使用命名空间时出现多个定义错误
- GCC 和非命名空间范围内的显式专用化
- 使用类指针重载C++命名空间函数模板专用化替代方法?
- 为什么在这种特殊情况下不需要将 std::hash() 的专用化注入到 std 命名空间中?
- 非命名空间范围内的显式专用化不会在 GCC 中编译
- 不同命名空间中函数的专用化
- BGL:不同命名空间中模板的专用化
- 模板专用化/初始化和命名空间
- 命名空间中所有类的类模板专用化
- 命名空间中的函数模板专用化
- 专用模板是命名空间别名实际上是一个别名
- 在std命名空间中添加模板专用化
- 为命名空间中的类专用化方法模板
- 警告:不同命名空间中的模板专用化
- 内部结构在非命名空间作用域中的显式专用化