C++具有多个类型列表的可变参数函数模板
编译器资源管理器演示显示了我发现的有效内容以及显示我想要的注释部分,但这行不通。
我是 C++ 新手,我正在尝试用 C++20 编写一个 sqlite3 接口,该接口对查询参数和返回的列类型进行类型检查。我已经被困了好几天了,读了这么多。我确信其中之一有我的答案,但我只是不太了解这些东西,无法弄清楚我的问题是什么:
最终,这就是我想要的 任何意见
template <class... T>
struct Typelist {};
struct Database {
Database(const string &sql) {}
template <class Input, class Output>
void Query(Input input) {}
// error: non-class, non-variable partial specialization
// 'Query<Typelist<Inputs ...>, Typelist<Outputs ...> >' is not allowed
template <class... Inputs, class... Outputs>
vector<tuple<Outputs...>> Query<Typelist<Inputs...>, Typelist<Outputs...>>(
const string &sql, Inputs... inputs) {}
};
int main() {
Database db(":memory:");
vector<tuple<string, string>> people =
db.Query<Typelist<int, float>, Typelist<string, string>>(
"SELECT fname, lname FROM users WHERE id = ? AND somefloat = ?;", 1, 42.0f);
}
将不胜感激。
Compiler Explorer Demonstration shows what I have found that works as well as a commented out section showing what I want, but that doesn't work.
I am new to C++ and I am trying to write a sqlite3 interface in C++20 that has type checking for query parameters and returned column types. I have been stuck for several days now and have read so much. I am sure that one of these has my answer, but I just don't understand this stuff well enough to figure out what my issue is:
- Variadic template constructor with multiple parameters packs
- template template variadic parameter pack
- C++ variadic template return type
- How can I have multiple parameter packs in a variadic template?
- Unpacking a typelist
Ultimately, this is what I would like to work
template <class... T>
struct Typelist {};
struct Database {
Database(const string &sql) {}
template <class Input, class Output>
void Query(Input input) {}
// error: non-class, non-variable partial specialization
// 'Query<Typelist<Inputs ...>, Typelist<Outputs ...> >' is not allowed
template <class... Inputs, class... Outputs>
vector<tuple<Outputs...>> Query<Typelist<Inputs...>, Typelist<Outputs...>>(
const string &sql, Inputs... inputs) {}
};
int main() {
Database db(":memory:");
vector<tuple<string, string>> people =
db.Query<Typelist<int, float>, Typelist<string, string>>(
"SELECT fname, lname FROM users WHERE id = ? AND somefloat = ?;", 1, 42.0f);
}
Any observations would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
continue
Are you sure that you need template specialization?
What about simply using overloading?
If you switch the
Input...
/Output...
in the template declarationyou can explicit the
Output...
list in the call and theInput...
list is deduced from theinputs...
argumentsYou can call
Query()
as followsObserve also that your compiler explore example compile if you simply rename
Query2()
asQuery()
.The following is a full compiling example
continue
Writing
std::format
is extremely hard. Use the same approach asstd::cout
- one object that you can<<
add to.May this short example written in a very short time show you how to use
sqlite3_str_appendf
to construct the query string and how to convert output parameters, but most importantly encourage learning a lot more about C++ programming. This is merely a template written in a very short time to show the interface, untessted - a real interface would be expected to be much more well thought through.