[英]using c++11 auto as return type for const function object
I have a const function object and for the timebeing, it returnes void. 我有一个const函数对象,并且对于时间,它返回无效。 but can return int or double.
但可以返回int或double。 I am writing the code in c++11 style and was just trying to use auto as return type.
我正在用c ++ 11样式编写代码,并且只是尝试使用auto作为返回类型。 Although the code compiles, I am not sure it is 100% correct or not.
虽然代码编译,但我不确定它是否100%正确。 Here is the code.
这是代码。
template <typename graph_t>
struct my_func {
public:
my_func() { }
my_func (graph_t& _G) : G(_G) { }
template <typename edge_t>
auto operator()(edge_t edge) -> void const {
//do something with the edge.
} //operator
private:
graph_t& G;
};
//call the functor: (pass graph G as template parameter)
std::for_each(beginEdge, endEdge, my_func<Graph>(G));
this code compiles and works in serial mode perfectly. 此代码完美地编译并在串行模式下工作。 Now I try to parallelize the above for_each using intel TBB parallel_for_each().
现在我尝试使用intel TBB parallel_for_each()并行化上面的for_each。 This requires the function object to be const.
这要求函数对象为const。 meaning the threads should not be allowed to modify or change the private variables of function object.
意味着不应该允许线程修改或更改函数对象的私有变量。
//tbb::parallel_for_each
tbb::paralle_for_each(beginEdge, endEdge, my_func<Graph>(G));
Now, the compiler error comes:
passing const my_func< ... > .. discards qualifiers
So I had to change the operator()() to the following: 所以我不得不将operator()()更改为以下内容:
template <typename edge_t>
void operator()(edge_t edge) const {
// do something
} //operator
My question is: how do i use "auto operator()() ->void" and also make the operator "const" so that it becomes valid ? 我的问题是:我如何使用“auto operator()() - > void”并使操作符“const”使其变为有效?
My question is: how do i use "auto operator()() ->void" and also make the operator "const" so that it becomes valid ?
我的问题是:我如何使用“auto operator()() - > void”并使操作符“const”使其变为有效?
template <typename edge_t>
auto operator()(edge_t edge) const -> void
{
//do something with the edge.
}
Remember, the declarator with a cv-qualifier has basically the following form: 请记住,具有cv-qualifier的声明符基本上具有以下形式:
( parameter-declaration-clause ) cv-qualifier-seq [ ref-qualifier ] [ exception-specification ] [ trailing-return-type ] ( parameter-declaration-clause ) cv-qualifier-seq [ ref-qualifier ] [ exception-specification ] [ trailing-return-type ]
(Omitted the attributes) (省略了属性)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.