簡體   English   中英

可變參數模板:錯誤:參數包未擴展為“…”

[英]Variadic template: error: parameter packs not expanded with '…'

我正在嘗試傳遞多個字符串以填充容器,但是我收到此錯誤。 使用gcc 4.9.3

template< class T >
struct DataCompare {
    bool operator()( const T& lhs, const T& rhs ) const
    {
        return operator<( lhs->getCode(), rhs->getCode() );
    }
};

using AggContainer = boost::container::flat_set< T, C >; 
using DataPtr      = boost::shared_ptr< BomQueueData >;
using PropertyQueueDataLess = DataCompare< DataPtr >;
using QueueDataPtrs = AggContainer< DataPtr, DataLess >;

QueueDataPtrs vector_name;

template< class Container, typename ... Args >
static void fillWithData(Container & oDataContainer, Args const & ... args)
{
    typedef typename Container::value_type::element_type QueueDataPtr;
    oDataContainer.emplace(new QueueDataPtr(args));
}

fillWithData(vector_name, x, a, b, c, d); // compiler error

怎么解決?

args是參數包,而不是參數。 這就是為什么您不能使用:

DataContainer.emplace(new QueueDataPtr(args));

相反,使用

DataContainer.emplace(new QueueDataPtr(args...));

這擴展了參數包。

為了實現完美的轉發,請對args參數使用通用引用,然后轉發它:

template< class Container, typename ... Args >
static void fillWithData(Container & oDataContainer, 
                         Args&& ... args)  // universal reference
{
    typedef typename Container::value_type::element_type QueueDataPtr;
    oDataContainer.emplace(new QueueDataPtr(std::forward<Args>(args)...));
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM