Suppose I have
struct SomeType {
template<typename... Args>
SomeType(Args... args);
// ...
};
template<typename... Args> std::tuple<Args> data; // syntax correct?
and want to construct a new object SomeType
from the argument values packed in the tuple
data
. How can I do that?
unique_ptr<SomeType> p = new SomeType( data ??? ); // how?
edit 1 I thought the question was pretty clear, but Kevin disagrees ... I want call the constructor of SomeBody
with the values packed in the tuple
data
as arguments. Got it, Keven?
edit 2 I thought a question is a question, but Kevin disagrees again. For his benefit, here is a use case: I want to dynamically construct thread-local objects, each constructed by its thread from some arguments provided earlier. Consider
template<typename Object>
class thread_specific
{
public:
// default constructor: objects will be default constructed
thread_specific();
// objects will be copy constructed from specimen provided
thread_specific(Object const&specimen);
// objects will be constructed from arguments provided
template<typename... Args>
thread_specifiv(Args... args);
// return thread-specific object; objects are constructed only when needed
Object& local_object();
};
(implemented, say, via a std::map<std::thread::id,unique_ptr<Object>>
). Now, if a global thread_specific<>
object is created by the user using the 3rd constructor, the arguments must be stored somehow and fed to the constructor of Object
when needed, i.e. at the first call to thread_specific<>::local_object()
on each std::thread
.