我有一些类都有一个带有文件路径的构造函数。我想从主函数中的 argv 参数创建一个 std::tuple 。这是一个草图
class A {
public:
A(const char *); // Taking a file path
};
class B {
public:
B(const char *); // Taking a file path
};
int main(int argc, char* argv[]) {
auto tup(myCreateTuple<A, B, A>(argc, argv));
// myCreateTuple would in this case use argv[1], argv[2]
// and argv[3] to create A(argv[1]), B(argv[2]) and A(argv[3]).
// argc is just passed along to verify that argv is long enough.
// tup would have of the type std::tuple<A ,B, A>
}
创建元组成员的顺序并不重要(即允许未定义构造函数的执行顺序)。你知道如何实现 myCreateTuple 吗?
我想有可能一起避免 myCreateTuple 而是使用
std::tuple<A, B, A> tup{ A(argv[1]), B(argv[2]), A(argv[3]) };
但这将是一个不太通用的解决方案。