运算符重载为lambda? [英] Operator overloading for lambdas?
问题描述
在重载操作符>>之前,我曾问过类似的问题,因为lambdas
但我没有解释我真正想要的。
I have asked a similar question before overloading operator >> for lambdas
But i did not explained what i really wanted .
我正在写一个关于sqlite3 C api的简单包装。
这是我的项目在github => sqlite modern cpp
I am writing a simple wrapper around sqlite3 C api .
this is my project on github => sqlite modern cpp
我想重载的>>运算符为lambda。
我想让休止代码工作:
I want to overload the >> operator for lambdas.
I want the fallowing code to work :
database db("dbfile.db");
db << "select age,name,weight from user where age > ? ;"
<< 18
>> [&](int age, string name, double weight) {
cout << age << ' ' << name << ' ' << weight << endl;
};
我想要的想法!这就是为什么我抽象的问题在我上一个问题。
我有一个 database_bind
类,由'<<'运算符返回,
i希望能够重载>> operator on database_bind
class
for lambdas with different number of arguments。
I want the idea! that is why i abstracted the question in my previous question.
I have a database_bind
class which is returned by the '<<' operator ,
i want to be able to overload >> operator on database_bind
class
for lambdas with different number of arguments.
目前我支持这种语法: / p>
Currently i'm supporting this syntax :
db << "select age,name,weight from user where age > ? ;"
<< 18
>> function<void(int,string,double)>([&](int age, string name, double weight) {
cout << age << ' ' << name << ' ' << weight << endl;
});
推荐答案
基于这个问题
i
我没有完全理解 function_traits
!但我能够
重载与不同数量的参数的lambdas的>>运算符。
我知道这不是最好的解决方案,但我写了它,所以有人可以把它作为起点(一个可变参数模板实现将是真棒!)。
Based on this question
i wrote the fallowing code.
I did not completely understood function_traits
! but i was able to
overload the >> operator for lambdas with different number of arguments .
I know it's not the best possible solution , but i wrote it so someone can take it as a starting point ( a variadic template implementation will be awesome !) .
#include<iostream>
#include<string>
#include<tuple>
using namespace std;
template <typename T>
struct function_traits
: public function_traits<decltype(&T::operator())>
{};
template <typename ClassType, typename ReturnType, typename... Args>
struct function_traits<ReturnType(ClassType::*)(Args...) const>
// we specialize for pointers to member function
{
enum { arity = sizeof...(Args) };
// arity is the number of arguments.
typedef ReturnType result_type;
template <size_t i>
struct arg
{
typedef typename std::tuple_element<i, std::tuple<Args...>>::type type;
// the i-th argument is equivalent to the i-th tuple element of a tuple
// composed of those arguments.
};
};
// a place holder class
class database_bind {};
template<int N>
class A {
template<typename F>
static void run(F l);
};
template<>
struct A<1> {
template<typename F>
static void run(F l) {
typedef function_traits<decltype(l)> traits;
typedef typename traits::arg<0>::type type_1;
type_1 col_1;
get_from_db(0, col_1);
l(col_1);
}
};
template<>
struct A<2> {
template<typename F>
static void run(F l) {
typedef function_traits<decltype(l)> traits;
typedef typename traits::arg<0>::type type_1;
typedef typename traits::arg<1>::type type_2;
type_1 col_1;
type_2 col_2;
get_from_db(0, col_1);
get_from_db(1, col_2);
l(col_1, col_2);
}
};
void get_from_db(int col_inx, string& str) { str = "string"; }
void get_from_db(int col_inx, int& i) {i = 123;}
void get_from_db(int col_inx, double& d) { d = 123.456; }
template<typename F>
void operator>>(database_bind dbb, F l)
{
typedef function_traits<decltype(l)> traits;
A<traits::arity>::run(l);
}
最后:
int main() {
database_bind dbb;
dbb >> [](int i, string s) { cout << i << ' ' << s << endl; };
dbb >> [](int i) { cout << i << endl; };
dbb >> [](string s,double d) { cout << s << ' ' << d << endl; };
}
这篇关于运算符重载为lambda?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!