简体   繁体   English

如何使类成员变量与函数模板的返回类型相同?

[英]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). 暂时将变量/数据属性声明为您知道返回类型不是(并且没有任何转换)的东西。

  • then read the error message from the compiler, and copy-paste the compiler's determination to the declaration. 然后从编译器中读取错误消息,并将编译器的确定复制粘贴到声明中。

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.

相关问题 返回类型不可知模板类成员函数 - Return type agnostic template class member function 如何专门针对模板类中的成员函数类型? - How to specialize for member function type in template class? 类函数的模板变量返回类型 - Template variable return type for a class function 如何将具有不同成员变量类型和不同构造函数的两个类制作成派生/基类或模板类? - How to make two classes with different member variable type and different constructor into derived/base class or template class? 模板 class 的 C++ 成员 function 的自动返回类型 - auto return type for a C++ member function of a template class 具有不同返回类型的显式专业化模板类成员函数 - explicit specialization template class member function with different return type 在成员函数返回类型上参数化的类模板部分特化 - class template partial specialization parametrized on member function return type Constexpr类模板成员函数与推导的void返回类型? - Constexpr class template member function with deduced void return type? 完美转发模板类成员函数的返回类型 - Perfect forwarding return type of a template class member function 如何获取函数返回类型的解除引用类型的模板成员 - How to get dereferenced type of template member for function return type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM