c++类型定义需要::type
C++ typedefs requiring ::type
尝试从BGL开始,这意味着我要从很多类型开始:
#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;
}
我想知道为什么EdgeWeightMap
的类型定义中的::type
是必要的。如果省略它,得到
错误:' operator[] '不匹配(操作数类型为
)
‘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>}’
和
‘Edge {aka boost::detail::edge_desc_impl<boost::undirected_tag, unsigned int>}’
)[e] = w;
(抱歉格式,类型' <
和>
似乎干扰了blockquote)
事实上,当我尝试
EdgeWeightMap weight_of = get(edge_weight,g);
我得到一个
错误:从
转换
‘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>}’
转换为非标量类型
‘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>}’
请求
edge_weight = get(ge_weight,g);
现在,我可以看到这些是不同的类型,我不明白的是为什么它们不同。由于我有点想避免意外,谁能告诉我什么时候需要::type
,什么时候不能使用它?
从property_map文档中可以看出,"property_map"模板被设计为提供类型,而不是类型。property_map结构恰好包含两种类型。type
和const_type
,分别用于可变和不可变类型
相关文章:
- 在类型和包装器之间reinterpret_cast是否安全<Type>?
- std::unordered_map 类型对象声明期间出现"field has incomplete type"错误
- 在"结构提升::enable_if<提升::is_pod<T>,无效>"中没有名为"type"的类型
- C++ "错误:在'类 std::result_of< ... >"中没有名为'type'的类型"
- Cython:如何从C级类型获取'actual Python type'(类型代码/dtype)
- 作为模板类型参数,为什么 type[N] 与其专用版本不匹配----模板<类 T>类 S<T[]>
- Clang 对使用的类型别名发出"unused type alias"警告
- 检测<T>某些非数字类型 T 的 std::numeric::type 的特化
- 不同指针类型之间的无效比较:'type*' 和 'type&(*)()'
- 按参数类型更改"return type template"
- 在shared_from_this()中的错误类型(在继承类中)(是否有dyn.type-ware shared Poi
- grpc & protobuf -- 错误:std::result_of<> 中没有名为 'type' 的类型
- 类 std::result 中没有名为 "type" 的类型
- 访问基类型数组成员(Int-to-type习惯用法)
- 类类型的"Incomplete type isn't allowed"数组
- `type'和`const type'的类型特征
- 我是否可以保证SizeOf(type)== sizeof(无符号类型)
- 编译错误C++与上面定义的发生错误的类型"does not name a type"?
- 仅当我提供类型检查规则时才"No type checking rule"错误
- reinterpret_cast<类型ID>"type-id"可以是变量吗?