在C++中使用decltype(),auto或RTTI类型进行等式测试?Boost是否有适合自己的东西?

Bre*_*ier 2 c++ sql type-inference rtti decltype

我正在编写一些代码来将C++类型转换为适合SQL DB的类型.我想识别类型,然后根据它是什么,生成适当的SQL代码.我不确定使用RTTI,auto或decltype可以在这方面做些什么.我有一些想法,但我不确定它们是否可行.

例如(我知道以下可能不是有效的C++,我只是试图了解这个想法):

if (decltype(some_var) == int) { do_stuff(); }
Run Code Online (Sandbox Code Playgroud)

要么

if (decltype(some_var) == decltype(1) { do_stuff(); }
Run Code Online (Sandbox Code Playgroud)

要么

switch(decltype(some_var)) {
    case int:
        do_int_stuff();
        break;
    case string;
        do_string_stuff();
        break;
    case bool;
        do_bool_stuff();
        break;
}
Run Code Online (Sandbox Code Playgroud)

要么

string get_func_y(int var) {
    ...
    return my_string;
}

string get_func_y(string var) {
    ...
    return my_string;
}

string get_func_y(bool var) {
    ...
    return my_string;
}

...
string SQL = get_func_y(some_var);
Run Code Online (Sandbox Code Playgroud)

任何这看起来都会起作用,或者有没有人就如何解决这个问题提出建议?提前感谢您提供的任何输入.

Dav*_*eas 5

您可以使用简单的元编程功能来确定(在编译时)两种类型是否相同:

template <typename T, typename U>
struct same_type 
{
   static const bool value = false;
};
template <typename T>
struct same_type< T, T >
{
   static const bool value = true;
};
Run Code Online (Sandbox Code Playgroud)

这实际上是否对您的程序有所帮​​助是一个不同的问题.我只想找到简单的函数重载解决方案.