回答

收藏

使用C 中的decltype(),auto或RTTI类型相等性测试?Boost对此有帮助吗

技术问答 技术问答 99 人阅读 | 0 人回复 | 2023-09-13

我正在编写一些代码,我C    类型转换适合SQL4 J2 Z/ S  H6 n& W4 S! W
DB类型。我想确定类型,然后根据类型生成合适的类型SQL我不确定使用代码RTTI,auto或decltype这方面能做什么?我有一些想法,但不确定是否可行。
- n( _/ h' u! M: c例如(我知道以下内容可能不有效C  ,我只想理解):
' C( z9 {: f2 R1 z' T; X0 W5 xif (decltype(some_var) == int) { do_stuff(); }或者+ o# g- Q+ v% G- J, I. W9 V* M% x
if (decltype(some_var) == decltype(1) { do_stuff(); }或者
' B: O  O& ]% [& [0 l, u1 @switch(decltype(some_var))    case int:        do_int_stuff();;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;break;    case string;        do_string_stuff();;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;break;    case bool;        do_bool_stuff();;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;break;}或者0 F7 ?( N9 m7 \; O% I, R( V% B. R4 Y
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);这看似可行,还是有人对如何解决这个问题有任何建议?感谢您提前提供的任何输入。
6 h7 d$ P/ d; q0 c, W* E8 T' a                                                               
9 o) u3 J* X5 y3 e! U, \. R; i/ u    解决方案:                                                               
% l8 Y8 t, }; Y$ ?4 ^% u                                                                您可以使用简单的元编程函数来确定两种类型是否相同:! R" U! M- \( T% P- g/ @& W
template struct same_type {   static const bool value = false;};template struct same_type{   static const bool value = true;};另一个问题是它是否真的对你的程序有帮助。我只是想找到一个简单的函数重载解决方案。
分享到:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则