简体   繁体   English

如何使用 sqlite_modern_cpp 为“select *”的 sql 查询编写 lambda?

[英]How do I write lambda for sql query of "select * " with sqlite_modern_cpp?

sqlite_modern_cpp examples sqlite_modern_cpp 示例

Usually we can output columns like this,通常我们可以像这样输出列,

db << "select age,name,weight from user where age > ? ;"
         << 18
         >> [&](int age, string name, double weight) {
            cout << age << ' ' << name << ' ' << weight << endl;
         };

But if the column number is unsure select * , how do I write the lambda?但是如果列号不确定select * ,我该如何写 lambda?

if the column number is unsure select *, how do I write the lambda如果列号不确定选择 *,我该如何编写 lambda

That's the answer here: you can't.这就是答案:你不能。 This is a fundamental rule of C++: the types of all objects are known at compile time.这是 C++ 的基本规则:所有对象的类型在编译时都是已知的。 There are no exceptions.没有例外。

This includes lambdas.这包括 lambda。 A lambda is just an instance of an anonymous class with an operator() overload, that's what it really is. lambda 只是一个带有operator()重载的匿名类的实例,这就是它的真正含义。 But it's still an instance of a discrete type that's defined at compile time.但它仍然是在编译时定义的离散类型的实例。

You can look into this API and, perhaps, determine if it allows you to represent a variable number of columns in a resultset as a std::vector , or something similar.您可以查看此 API,并可能确定它是否允许您将结果集中的可变数量的列表示为std::vector或类似的东西。 That would be one way to retrieve a list of an unknown number of columns at runtime, your lambda would then have to figure out what to do with them.这将是在运行时检索未知数量的列列表的一种方法,然后您的 lambda 必须弄清楚如何处理它们。

But in terms of enumerating columns as discrete parameters to a function or a lambda: that's up to you.但就将列枚举为函数或 lambda 的离散参数而言:这取决于您。 You must specify explicit parameters, and their types, at compile time.您必须在编译时指定显式参数及其类型。 C++ does not really work in any other way. C++ 并没有真正以任何其他方式工作。 Variadic lambdas, and templates, are just templates.可变参数 lambda 和模板只是模板。 Templates are not discrete types, they're just templates for types.模板不是离散类型,它们只是类型的模板。 They still need to be instantiated at compile time to a discrete type.它们仍然需要在编译时实例化为离散类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM