I want to store pointers to copies of parameter pack arguments in a tuple. Here is the code:
struct FDead {};
struct FAlive {};
struct FBossDead final : FDead {};
struct FBossAlive final : FAlive {};
template<typename... TStates>
struct TContext
{
using FTuple = std::tuple<TStates*...>;
template<typename... TSubStates>
explicit TContext(TSubStates&&... InStates)
{
static_assert(sizeof...(TStates) == sizeof...(TSubStates));
// FIXME: Check if TSubStates are actually sub-types of TStates
//static_assert(((std::is_base_of_v<TStates, TSubStates> || ...) && ...));
States = FTuple{(new TSubStates{ InStates }, ...)};
}
FTuple States;
};
void Test()
{
TContext<FAlive, FDead> Context
{
FBossAlive{},
FBossDead{}
};
}
As you can see, FBossDead
extends FDead
, and FBossAlive
extends FAlive
. TContext
is created with base types as template arguments but then I'm sending their subtypes that I want to copy and then store pointers to them in the States
tuple.
I am getting this compilation error though:
[C2440] '<function-style-cast>': cannot convert from 'initializer list' to 'std::tuple<PCF::SubClass::FAlive *,PCF::SubClass::FDead *>'
I believe that's because of this fold expression:
(new TSubStates{ InStates }, ...)
that evaluates to a initializer_list
, not a tuple (because of the comma, I believe) but I have no idea how to fix this problem. Any help will be much appreciated!
n.b. I need to store copies, I can not change the constructor signature to accept a pack of pointers.
CodePudding user response:
You don't need a fold expression here. A regular parameter pack expansion will do the trick just fine.
Also, while not strictly necessary for your example as posted, using std::forward<>
when dealing with Forwarding References (which InStates
is) is a good habit to get into.
States = FTuple{ new TSubStates{ std::forward<TSubStates>(InStates) }... };
But you might as well do it in the initializer list:
template<typename... TSubStates>
explicit TContext(TSubStates&&... InStates)
: States{ new TSubStates{ std::forward<TSubStates>(InStates) }... } {
// FIXED: Check if TSubStates are actually sub-types of TStates
// But this is redundant, as the pointer assignment itself would fail.
static_assert((std::is_base_of_v<TStates, TSubStates> && ...));
}