使用c ++ 11 auto作为const函数对象的返回类型

Pog*_*ogo 5 c++ tbb boost-graph c++11

我有一个const函数对象,并且对于时间,它返回无效.但可以返回int或double.我正在用c ++ 11样式编写代码,并且只是尝试使用auto作为返回类型.虽然代码编译,但我不确定它是否100%正确.这是代码.

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));
Run Code Online (Sandbox Code Playgroud)

此代码完美地编译并在串行模式下工作.现在我尝试使用intel TBB parallel_for_each()并行化上面的for_each.这要求函数对象为const.意味着不应该允许线程修改或更改函数对象的私有变量.

   //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
Run Code Online (Sandbox Code Playgroud)

所以我不得不将operator()()更改为以下内容:

   template <typename edge_t>
   void operator()(edge_t  edge) const { 

   // do something

   } //operator
Run Code Online (Sandbox Code Playgroud)

我的问题是:我如何使用"auto operator()() - > void"并使操作符"const"使其变为有效?

Col*_*mbo 2

我的问题是:我如何使用“auto operator()() ->void”并使运算符“const”使其变得有效?

   template <typename edge_t>
   auto operator()(edge_t   edge) const -> void
   {

     //do something with the edge.
   }
Run Code Online (Sandbox Code Playgroud)

请记住,带有 cv 限定符的声明符基本上具有以下形式:

(参数声明子句) cv-qualifier-seq [ ref-qualifier ] [异常规范] [尾随返回类型]

(省略属性)