在C ++中类型相等测试与decltype(),auto或RTTI? Boost有什么东西吗? [英] Type equality test w/ decltype(), auto, or RTTI in C++? Does Boost have something for this?

查看:138
本文介绍了在C ++中类型相等测试与decltype(),auto或RTTI? Boost有什么东西吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一些代码,将C ++类型转换为SQL DB的相应类型。我想识别的类型,然后根据它是什么,产生适当的SQL代码。我不知道在这方面可以做什么使用RTTI,auto或decltype。我有一些想法,但我不知道如果他们是可行的。

I'm writing some code to translate a C++ type to an appropriate type for a SQL DB. I want to identify the type, and then depending on what it is, produce the appropriate SQL code. I'm not sure exactly what can be done in this regard by using RTTI, auto, or decltype. I have some ideas but I'm not sure if they're workable.

例如(我知道以下可能不是有效的C + +,我只是试图

For instance (I know the following may not be valid C++, I'm just trying to get the idea across):

if (decltype(some_var) == int) { do_stuff(); }

if (decltype(some_var) == decltype(1) { do_stuff(); }

switch(decltype(some_var)) {
    case int:
        do_int_stuff();
        break;
    case string;
        do_string_stuff();
        break;
    case bool;
        do_bool_stuff();
        break;
}

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);



Any of this look like it would work, or does anyone have advice on how to go about this? Thanks ahead of time for any input you may have.

推荐答案

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

You can use a simple metaprogramming function to determine (at compile time) whether two types are the same:

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;
};

这是否真的能帮助你的程序是一个不同的问题。我只是去简单的函数重载解决方案。

Whether that actually helps you with your program or not is a different question. I would just go for the simple function overload solution.

这篇关于在C ++中类型相等测试与decltype(),auto或RTTI? Boost有什么东西吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆