简体   繁体   English

需要:: type的C ++ typedef

[英]C++ typedefs requiring ::type

Trying to start with BGL, which means I'm starting with a lot of typedefs: 尝试从BGL开始,这意味着我从很多typedef开始:

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

And I was wondering just why the ::type in the typedef for EdgeWeightMap was necessary. 而且我想知道为什么EdgeWeightMap的typedef中的::type是必需的。 If I omit it, I get 如果我忽略它,我得到

error: no match for 'operator[]' (operand types are 错误:“ 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>}’

and

‘Edge {aka boost::detail::edge_desc_impl<boost::undirected_tag, unsigned int>}’

) weight_of[e] = w; )weight_of [e] = w;

(Sorry about the format, the typedefs' < and > seem to be interfering with the blockquote) (抱歉,格式,typedef的<>似乎干扰了blockquote)

And indeed, when I try 确实,当我尝试

EdgeWeightMap weight_of = get(edge_weight,g);

I get an 我得到一个

error: conversion from 错误:转换自

‘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>}’

to non-scalar type 到非标量类型

‘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>}’

requested EdgeWeightMap weight_of = get(edge_weight,g); 请求的EdgeWeightMap weight_of = get(edge_weight,g);

Now, I can see these are different types, what I don't get is why they differ. 现在,我看到这些是不同的类型,但我不明白的是它们为何不同。 And as I kind of would prefer to avoid surprises, could somebody please enlighten me as to when the ::type is required and when it must not be used? 正如我希望避免出现意外那样,有人可以启发我:什么时候需要::type以及什么时候不可以使用::type

From reading the property_map documentation, the "property_map" template is designed to provide types, not be a type. 通过阅读property_map文档,“ property_map”模板旨在提供类型,而不是类型。 The property_map structure contains exactly two types. property_map结构恰好包含两种类型。 type and const_type , for mutable and immutable types respectively. typeconst_type ,分别用于可变和不可变类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM