Как пересылать параметры конструктора в содержащийся кортеж, когда один из элементов кортежа имеет конструктор по умолчаC++

Программы на C++. Форум разработчиков
Anonymous
Как пересылать параметры конструктора в содержащийся кортеж, когда один из элементов кортежа имеет конструктор по умолча

Сообщение Anonymous »

Я пытаюсь написать класс, который использует std::tuple для хранения множества объектов потенциально разных типов. Если все элементы имеют векторы, принимающие какой-либо параметр, я могу правильно инициализировать кортеж из вектора контейнера. Однако если у одного из элементов есть только вектор по умолчанию, а у других нет, я не смогу построить кортеж. Я получаю ошибку компиляции.
Вот код:

Код: Выделить всё

struct Op1WithArg
{
Op1WithArg(int){}
};

struct Op2WithArg
{
Op2WithArg(std::string){}
};

struct OpWithoutArg
{
OpWithoutArg(){}
};

template 
struct OpsSequence {
using OpTuple = std::tuple;

template
OpsSequence(Args... args)
:   mOps{args...}
{}

OpTuple mOps;
};

void TestSequenceWithOpWithoutArg()
{
// This works
using SequenceType = OpsSequence;
SequenceType sequence{};
}

void TestSequenceWithTwoOpsWithArg()
{
// This works
using SequenceType = OpsSequence;
SequenceType sequence{1, "Hello"};
}

void TestSequenceWithOneOpWithArgAndOneWithout()
{
// This fails to compile
using SequenceType = OpsSequence;
SequenceType sequence{1,{}};
}

Ошибка компиляции конечной функции следующая:

Код: Выделить всё

|| [ 50%] Building CXX object CMakeFiles/tsconv_tests.dir/tests/tsconv_tests.cpp.o
|| /home/anders/src/tsconv/tests/tsconv_tests.cpp: In function ‘void tsconv::TestSequenceWithOneOpWithArgAndOneWithout()’:
tests/tsconv_tests.cpp|1014 col 31| error: no matching function for call to ‘tsconv::OpsSequence::OpsSequence(
)’
||  1014 |     SequenceType sequence{1,{}};
||       |                               ^
tests/tsconv_tests.cpp|989 col 5| note: candidate: ‘tsconv::OpsSequence::OpsSequence(Args ...) [with Args = {}; Ops = {tsconv::Op1WithArg, tsconv::OpWithoutArg}]’
||   989 |     OpsSequence(Args... args)
||       |     ^~~~~~~~~~~
tests/tsconv_tests.cpp|989 col 5| note:   candidate expects 0 arguments, 2 provided
tests/tsconv_tests.cpp|985 col 8| note: candidate: ‘constexpr tsconv::OpsSequence::OpsSequence(const tsconv::OpsSequence&)’
||   985 | struct OpsSequence {
||       |        ^~~~~~~~~~~
tests/tsconv_tests.cpp|985 col 8| note:   candidate expects 1 argument, 2 provided
tests/tsconv_tests.cpp|985 col 8| note: candidate: ‘constexpr tsconv::OpsSequence::OpsSequence(tsconv::OpsSequence&&)’
tests/tsconv_tests.cpp|985 col 8| note:   candidate expects 1 argument, 2 provided
gmake[2]: *** [CMakeFiles/tsconv_tests.dir/build.make|76| CMakeFiles/tsconv_tests.dir/tests/tsconv_tests.cpp.o] Error 1
gmake[1]: *** [CMakeFiles/Makefile2|83| CMakeFiles/tsconv_tests.dir/all] Error 2
gmake: *** [Makefile|101| all] Error 2
Можно ли изменить код, чтобы скомпилировать неработающую функцию?

Подробнее здесь: https://stackoverflow.com/questions/787 ... -the-tuple

Вернуться в «C++»