简体   繁体   English

模板函数中的获取/推导类型以获取返回值

[英]Getting/deducing type in template-function for return value

I am using the Boost Graph Library and have build my own classes upon that. 我正在使用Boost Graph Library,并在此基础上构建了自己的类。

Now, I have the following function: 现在,我具有以下功能:

template< class VERTEXPROPERTIES >
VERTEXPROPERTIES& properties(const Vertex& v) const
{   
    typename property_map<FilteredGraphContainer, vertex_properties_t>::const_type param = get(vertex_properties, graph_);

    return param[v];
}

When I use this function in a class template like this: 当我在这样的类模板中使用此函数时:

template< class VERTEXPROPERTIES, class EDGEPROPERTIES >
class Graph
{
// all the code
};

it works. 有用。 However, when I want to use it in a class like this: 但是,当我想在这样的类中使用它时:

template < class GRAPH, class EDGE_PREDICATE, class VERTEX_PREDICATE >
class FilteredGraph
{
// all the code
};

and apply the properties() function to a FilteredGraph object, the compiler complains that it can't find the function. 并将properties()函数应用于FilteredGraph对象,编译器会抱怨找不到该函数。
As far as I have found out so far this is probably due to the missing template-parameter in the arguments list. 据我到目前为止发现,这可能是由于参数列表中缺少模板参数。
For the Graph-class, this template-parameter is easy to find out, as it gets defined when creating a Graph-object. 对于Graph类,此模板参数很容易找出,因为它是在创建Graph对象时定义的。
But for the FilteredGraph, it should be able to deduce it from the GRAPH-template parameter. 但是对于FilteredGraph,它应该能够从GRAPH-template参数推导出来。
Then again, I think that the compiler nevertheless will have problems getting the right return-value type as this might probably be necessary to find out beforehand... 再说一遍,我认为编译器仍然会在获取正确的返回值类型方面遇到问题,因为这可能是事先需要了解的...
I would really like to keep that concept with the properties() function as it allows to keep the other functions generic. 我真的很想将这个概念与properties()函数保持一致,因为它允许保持其他函数通用。
Do you have any ideas how to solve that issue, possibly keeping the concept? 您是否有解决该问题的想法,并可能保留该想法?

Again, my apologies for not providing more information. 再次,我很抱歉没有提供更多信息。
It seemed to me that the compiler was not able to deduce the dependant type there. 在我看来,编译器无法在此处推断出依赖类型。 I encountered this issue some times before when working with templates. 在使用模板之前,我曾几次遇到此问题。
I will provide the fix here in case somebody might encounter similar issues. 如果有人遇到类似问题,我将在此处提供修复程序。

The FilteredGraph uses an instance of Graph (basically an adjacency_list with internal custom properties) as its input. FilteredGraph使用Graph的实例(基本上是具有内部自定义属性的adjacency_list)作为其输入。 The Graph-class has two template parameters, one for the properties of the vertices and one for the properties of the edges. Graph类具有两个模板参数,一个用于顶点的属性,一个用于边的属性。
As a little sidenote, using bundled properties (not supported by older version of boost graph library and also not by some compilers), I think, this problem could be circumvented as you would not need an extra function returning a value of a(possibly) unknown type. 作为一点说明,使用捆绑属性(boost图形库的较早版本不支持,某些编译器也不支持),我认为,可以避免此问题,因为您不需要额外的函数来返回a值(可能)类型未知。 Because the Graph-object passed to the FilteredGraph needs to be specified for some particular types of vertex/edge properties, this information can actually be extracted. 因为需要为某些特定类型的顶点/边属性指定传递给FilteredGraph的Graph对象,所以实际上可以提取此信息。
I achieved this via: 我是通过以下方式实现的:

/// Type of the internal properties of the edges
typedef typename property_traits< typename property_map<FilteredGraphContainer, vertex_properties_t>::type >::value_type VProps;
/// Type of the internal properties of the edges
typedef typename property_traits< typename property_map<FilteredGraphContainer, edge_properties_t>::type >::value_type EProps;

and then defining: 然后定义:

VProps& properties(const Vertex& v)
{
        typename property_map<FilteredGraphContainer, vertex_properties_t>::type param = get(vertex_properties, graph_);
        return (param[v]);
}

Thanks again for your interest in this question and I am sorry not having provided more information. 再次感谢您对这个问题的关注,很抱歉没有提供更多信息。

Best regards. 最好的祝福。

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

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