如果构造函数的执行顺序很重要,我如何使用std :: make_tuple?
例如,我猜A类的构造函数和类B的构造函数的执行顺序未定义为:
std::tuple<A, B> t(std::make_tuple(A(std::cin), B(std::cin)));
Run Code Online (Sandbox Code Playgroud)
在阅读了对该问题的评论后,我得出了这个结论
这就是说
template<typename... args>
std::tuple<args...> parse(std::istream &stream) {
return std::make_tuple(args(stream)...);
}
Run Code Online (Sandbox Code Playgroud)
实现具有未定义的构造函数的执行顺序.
更新,提供一些上下文:
为了给我想要做的更多背景,这里是一个草图:
我想在CodeSynthesis XSD二进制解析/序列化的帮助下从stdin中读取一些序列化对象.以下是如何完成此类解析和序列化的示例:example/cxx/tree/binary/xdr/driver.cxx
xml_schema::istream<XDR> ixdr (xdr);
std::auto_ptr<catalog> copy (new catalog (ixdr));
Run Code Online (Sandbox Code Playgroud)
我希望能够指定序列化对象具有的类的列表(例如,目录,目录,3个序列化对象的someOtherSerializableClass)并将该信息存储为typedef
template <typename... Args>
struct variadic_typedef {};
typedef variadic_typedef<catalog, catalog, someOtherSerializableClass> myTypes;
Run Code Online (Sandbox Code Playgroud)
并在解析完成后找到一种方法来使用std :: tuple.草图:
auto serializedObjects(binaryParse<myTypes>(std::cin));
Run Code Online (Sandbox Code Playgroud)
其中serializedObjects将具有该类型
std::tuple<catalog, catalog, someOtherSerializableClass>
Run Code Online (Sandbox Code Playgroud) 假设我有一些像这样的结构:
struct MyStruct1 {
inline void DoSomething() {
cout << "I'm number one!" << endl;
}
};
struct MyStruct2 {
static int DoSomething() {
cout << "I'm the runner up." << endl;
return 1;
}
};
struct MyStruct3 {
void (*DoSomething)();
MyStruct3() {
DoSomething = &InternalFunction;
}
static void InternalFunction() {
cout << "I'm the tricky loser." << endl;
}
};
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,对于所有三个结构,我可以在该结构的对象上调用DoSomething()并使其工作(尽管每个结构的实现方式不同):
MyStruct1 a;
MyStruct2 b;
MyStruct3 c;
a.DoSomething(); // works, calls Struct1's instance function
b.DoSomething(); // works, calls Struct2's …Run Code Online (Sandbox Code Playgroud) 是否可以在运行时决定调用哪个模板函数?就像是:
template<int I>
struct A {
static void foo() {/*...*/}
};
void bar(int i) {
A<i>::f(); // <-- ???
}
Run Code Online (Sandbox Code Playgroud) 我希望能够做到这一点:
template<typename Mix>
struct A {
A(int i) { }
};
template<typename Mix>
struct B {
B() { }
B(const char*) { }
};
template<template<typename> class... Mixins>
struct Mix : Mixins<Mix<Mixins...>>... {
// This works, but forces constructors to take tuples
template<typename... Packs>
Mix(Packs... packs) : Packs::Type(packs.constructorArgs)... { }
};
template<template<typename> class MixinType, typename... Args>
struct ArgPack {
typedef MixinType Type; // pretend this is actually a template alias
tuple<Args...> constructorArgs;
ArgPack(Args... args) : constructorArgs(args...) { }
}
template<typename... Args> …Run Code Online (Sandbox Code Playgroud) 在C++ 11中,lambda函数是一个对象,应该可以用它来调用make_tuple,对吗?
void foobar() {
auto t = std::make_tuple([](){ std::make_shared<int>(); });
}
Run Code Online (Sandbox Code Playgroud)
这段代码适合我.
现在,如果我们添加一个可变参数模板会发生什么:
#include <tuple>
#include <memory>
template <class... T>
void foobar() {
auto t = std::make_tuple([](){ std::make_shared<T>(); }...);
}
int main(int, char**)
{
foobar<int, float, double>();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这个无法在GCC 4.7.2中编译
main.cpp: In lambda function:
main.cpp:6:54: error: parameter packs not expanded with '...':
main.cpp:6:54: note: 'T'
main.cpp: In function 'void foobar()':
main.cpp:6:57: error: expansion pattern '#'lambda_expr' not supported by dump_expr#<expression error>' contains no argument packs
main.cpp: In instantiation …Run Code Online (Sandbox Code Playgroud)