今天我遇到了一个有趣的问题.我创建了一个lambda,它接受对struct的引用,并错误地将它设置为std :: function,它按值接收它的参数.
这是一个更简洁的版本:
#include <functional>
struct InputStruct
{
int i;
InputStruct(): i(1){}
};
void function_rcv(std::function<bool(InputStruct)> & func_ref)
{
InputStruct in;
func_ref(in);
}
int main()
{
std::function<bool(InputStruct)> my_func = [](InputStruct & in)->bool{return in.i==1;};
function_rcv(my_func);
}
Run Code Online (Sandbox Code Playgroud)
用godbolt检查显示这与MSVC成功编译,但对于Clang和GCC都失败了.
有趣的是,使用原语而不是结构会在所有三个编译器上编译失败.
这是MSVC编译器中的错误吗?
我有3个表,让我们说表A,B,C来混淆我的软件:).A和B有两列数字值,表C有一列布尔列.
我想要的是创建一个具有单个列的视图,其中根据C中的列,选择A或B中的值.
例:
输入:
| A.val | | B.val | | C.val |
--------- --------- ---------
entry1 | 1 | | 6 | | T |
entry2 | 2 | | 8 | | F |
Run Code Online (Sandbox Code Playgroud)
输出:
| D |
-----
entry1 | 1 |
entry2 | 8 |
Run Code Online (Sandbox Code Playgroud)
我想知道是否有办法在SQL语句中执行此操作,因为我目前正在以编程方式执行此操作,这会占用不必要的资源.
我试图在这里实现的情况是一个Base类,它有一个函数(让我们称之为modify_command),它几乎可以接受许多不同的类型,因此派生类可以根据需要实现modify_command函数.现在我在基类中有这些行:
class Base
{
template<typename Command>
void modify_command(Command cmd)
{
std::cout << "Modify command called with unimplemented command type:" << typeid(cmd).name();
}
virtual void modify_command(SpecificCommandA cmd)
{
modify_command<SpecificCommandA>(cmd); // Calls the templated function
}
virtual void modify_command(SpecificCommandB cmd)
{
modify_command<SpecificCommandB>(cmd); // Calls the templated function
}
// etc.
};
Run Code Online (Sandbox Code Playgroud)
然后在派生类中:
class Derived : public Base
{
virtual void modify_command(SpecificCommandA cmd)
{
cmd.x = 1;
cmd.y = 2;
}
}
Run Code Online (Sandbox Code Playgroud)
显然虚拟模板函数不是可能的,所以在某种形式下,我将不得不列出函数声明,以便进行多种参数可能性,这肯定会使类定义混乱,并且可能使其他命令类型变得困难(随着时间的推移)被处理
具有模板化函数的目的是为了编译这种情况而无需定义modify_command(SpecificCommandC)来记录错误:
Base * base = new Derived();
SpecificCommandA a;
SpecificCommandB …Run Code Online (Sandbox Code Playgroud)