可变参数模板:将整数参数完美转发到lambda

时间:2018-09-05 10:03:15

标签: c++ c++14

也有类似的问题,但我找不到适合我问题的答案。

考虑以下代码:

#include <cassert>
#include <functional>
#include <iostream>
#include <memory>
#include <utility>

class TestClass
{
public:
   TestClass( int value): mValue( value) { }
private:
   int  mValue;
};

template< typename T> class DeferredCreator
{
public:
   template< class... Args> DeferredCreator( Args&&... args):
      mpCreator( [=]() -> T*
         { return new T( std::forward< Args>( args)...);  }
      ),
      mpObject()
   { }

   T* get() {
      if (mpObject == nullptr)
         mpObject.reset( mpCreator());
      return mpObject.get();
   }
private:
   std::function< T*( void)>  mpCreator;
   std::unique_ptr< T>        mpObject;
};


int main() {
   DeferredCreator< int>  dcInt( 42);

   assert( dcInt.get() != nullptr);
   return 0;
}

这个想法是,DeferredCreator类仅在确实需要时才创建对象。我得到了这项工作,例如字符串,但是我不知道如何将一个简单的整数传递给我的lambda。

我收到的错误消息是:

prog.cpp:19:26: error: no matching function for call to 'forward'
         { return new T( std::forward< Args>( args)...);  }
                         ^~~~~~~~~~~~~~~~~~~
prog.cpp:36:27: note: in instantiation of function template specialization 'DeferredCreator<int>::DeferredCreator<int>' requested here
   DeferredCreator< int>  dcInt( 42);
                          ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/bits/move.h:76:5: note: candidate function not viable: 1st argument ('const int') would lose const qualifier
    forward(typename std::remove_reference<_Tp>::type& __t) noexcept
    ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/bits/move.h:87:5: note: candidate function not viable: 1st argument ('const int') would lose const qualifier
    forward(typename std::remove_reference<_Tp>::type&& __t) noexcept
    ^
2 errors generated.

我已经尝试将decltype( args)用作std::forward<>的模板参数,但这无济于事。

代码也可以在这里找到:https://ideone.com/MIhMkt

2 个答案:

答案 0 :(得分:6)

args...是常量,因为lambda的调用运算符是隐式const。因此,如果您使lambda可变,那么它将起作用:

[=]() mutable -> T*
     { return new T( std::forward< Args>( args)...);  }

之所以无法与decltype(args)配合使用,是因为类型本身不是const,而只是调用运算符。

答案 1 :(得分:4)

您的 lambda表达式生成的闭包类型operator()const限定的。 std::forward可以尝试移动args...,它们是闭包的数据成员。 const个对象无法移动。

您可以将lambda标记为mutable

  mpCreator( [=]() mutable -> T*
     { return new T( std::forward< Args>( args)...);  }
  ),

这将从闭包类型的生成的const中删除隐式operator()限定词。

live example on wandbox.org