rh0x
rh0x

Reputation: 1463

Variadic template: error: parameter packs not expanded with '...'

I'm trying to pass multiple strings to fill a Container, but I receive this error. Using 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

How to solve?

Upvotes: 3

Views: 6263

Answers (2)

Richard Hodges
Richard Hodges

Reputation: 69854

For perfect forwarding, use a universal reference for the args parameter, and then forward it:

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)...));
}

Upvotes: 3

R Sahu
R Sahu

Reputation: 206567

args is a parameter pack, not a parameter. That's why you can't use:

DataContainer.emplace(new QueueDataPtr(args));

Instead, use

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

That expands the parameter pack.

Upvotes: 6

Related Questions