[英]How to make a class member variable be the same type of return of a function template?
I am using a third party C++ library for sqlite orm. 我正在为sqlite orm使用第三方C ++库。 The library has a function that returns a template object.
该库具有一个返回模板对象的函数。 The type of the object is a very long template which depends on the number tables etc. That is why the examples provided always use
auto
. 对象的类型是一个很长的模板,取决于模板等。这就是为什么提供的示例始终使用
auto
。 Here is an example: 这是一个例子:
auto storage = make_storage("db.sqlite",
make_table("users",
make_column("id", &User::id, autoincrement(), primary_key()),
make_column("first_name", &User::firstName),
make_column("last_name", &User::lastName),
make_column("birth_date", &User::birthDate),
make_column("image_url", &User::imageUrl),
make_column("type_id", &User::typeId)),
make_table("user_types",
make_column("id", &UserType::id, autoincrement(), primary_key()),
make_column("name", &UserType::name, default_value("name_placeholder"))));
The type of the storage
object that is returned from the function is very long, something like this: 从该函数返回的
storage
对象的类型非常长,如下所示:
sqlite_orm::internal::storage_t<sqlite_orm::internal::table_t<Feed::MyClass, sqlite_orm::internal::column_t<Feed::MyClass, int, int const& (Feed::MyClass::*)() const, void (Feed::MyClass::*)(int), sqlite_orm::constraints::autoincrement ...
My question is that I would like to use this storage
object as a member variable of another class. 我的问题是我想将此
storage
对象用作另一个类的成员变量。 How would I tell the client class the return type of the function make_storage
? 我如何告诉客户端类
make_storage
的返回类型?
You can use decltype(expr)
to determine the type of an expression. 您可以使用
decltype(expr)
确定表达式的类型。 To reasonably get hold of this type I'd package the query into a function with an auto
result and use it to obtain the type for the member and later to actually do the queries: 为了合理地掌握这种类型,我将查询打包到具有
auto
结果的函数中,并使用它来获取成员的类型,然后再实际执行查询:
template <typename... Args>
auto make_storage_query() {
return make_storage("db.sqlite",
make_table("users",
make_column("id", &User::id, autoincrement(), primary_key()),
make_column("first_name", &User::firstName),
make_column("last_name", &User::lastName),
make_column("birth_date", &User::birthDate),
make_column("image_url", &User::imageUrl),
make_column("type_id", &User::typeId)),
make_table("user_types",
make_column("id", &UserType::id, autoincrement(), primary_key()),
make_column("name", &UserType::name, default_value("name_placeholder"))));
}
struct whatever {
decltype(make_storage_query()) member;
}
int main() {
whatever run{make_storage_query()};
}
Things get more interesting when your query needs arguments as you'll probably need to pass appropriate arguments to your query. 当查询需要参数时,事情会变得更加有趣,因为您可能需要将适当的参数传递给查询。 If so, you'd need to come up with suitable arguments in for use with
decltype(...)
. 如果是这样,则需要提供与
decltype(...)
一起使用的合适参数。 As you may need variables which wouldn't be available in this context you'd probably use std::declval<T>()
in place of an argument of type T
, eg, std::declval<int&>()
for an argument where a non- const
variable of type int
is passed. 由于您可能需要在这种情况下不可用的变量,因此您可能会使用
std::declval<T>()
代替类型T
的参数,例如, std::declval<int&>()
传递int
类型的非const
变量的参数。 However, I haven't used the library you are referring to, ie, I don't know what's actually needed or whether it makes sense to use the return type as member. 但是,我没有使用过您所引用的库,即,我不知道实际需要什么,也不知道使用return类型作为成员是否有意义。
How would I tell the client class the return type of the function make_storage?
我如何告诉客户端类make_storage函数的返回类型?
Just ask the compiler! 只是问编译器!
Temporarily declare your variable/data attribute as something you know the return type is not, (and there are no conversions for it). 暂时将变量/数据属性声明为您知道返回类型不是(并且没有任何转换)的东西。
The error message will report that it cannot find a conversion for the requested assignment. 错误消息将报告它找不到所请求分配的转换。
If g++ description is confusing, try the same compile using clang++. 如果g ++描述令人困惑,请尝试使用clang ++进行相同的编译。
// auto - temporarily try int or some other type
int storage = make_storage("db.sqlite",
make_table("users",
// the rest of it ....
make_column("name",
&UserType::name,
default_value("name_placeholder"))));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.