How could I extract and use the types defined in variable template arguments of a C++ class so that instead of defining multiple duplicates of the following class which defer by only the number and types of parameters:
template <typename T1, typename T2>
class SUTTestFixture1
{
public:
void SetUp(T1 expected, vector<T2> data)
{
_expected = expected;
_data = data;
}
protected:
SUT _sutObj;
vector<T2> _data;
T1 _expected;
};
template <typename T1, typename T2, typename T3>
class SUTTestFixture2
{
public:
void SetUp(T1 expected, T2 param, vector<T3> data)
{
_expected = expected;
_param = param;
_data = data;
}
protected:
SUT _sutObj;
vector<T2> _data;
T1 _expected;
T2 _param;
};
I expect to be able to just use a single class with variale template arguments:
template <typename... T>
class SUTTestFixture
{
public:
void SetUp(T... args)
{
_expected = args...[0];
_data = args...[1]; // expected ‘;’ before ‘...’ token
}
protected:
SUT _sutObj;
vector<T...> _data; // XXX
T... _expected; // error: expected unqualified-id before ‘...’ token
};
SUTTestFixture1
use case:
class TestFixture1 : public SUTTestFixture1<long, long>, public testing::TestWithParam<tuple<long, vector<long>>>
{
public:
void SetUp() override
{
RangeTestFixture1::SetUp(get<0>(GetParam()), get<1>(GetParam()));
}
long Function1Test()
{
return _sutObj.Function(_data);
}
};
SUTTestFixture2
use case:
class TestFixture2 : public SUTTestFixture2<size_t, size_t, size_t>, public testing::TestWithParam<tuple<size_t, size_t, vector<size_t>>>
{
public:
void SetUp() override
{
RangeTestFixture2::SetUp(get<0>(GetParam()), get<1>(GetParam()), get<2>(GetParam()));
}
size_t Function2Test()
{
return _sutObj.Function(_param, _data);
}
};
16