相关疑难解决方法(0)

C++是否已经有某种反思?

考虑这个例子:

struct Nobody_Expects_The_Spanish_Inquisition{};

int main(){
    throw Nobody_Expects_The_Spanish_Inquisition();
}
Run Code Online (Sandbox Code Playgroud)

Ideone上显示的输出:

抛出的一个实例后终止叫Nobody_Expects_The_Spanish_Inquisition "

Windows的类似输出:

Test.exe中0x760fb727处的未处理异常:Microsoft C++异常:内存位置0x001ffea3处的Nobody_Expects_The_Spanish_Inquisition

可以看出,最终的程序集似乎已经包含了异常的名称,或者有另一种获取名称的方法.

这可以被视为某种反思吗?或者,如果实际可以显示异常的名称,它是否依赖于编译器/ OS?

c++ reflection exception

6
推荐指数
1
解决办法
429
查看次数

按其值打印#define的名称?

我有一个C程序,其中包含一些错误代码的定义.像这样:

#define FILE_NOT_FOUND -2
#define FILE_INVALID -3 
#define INTERNAL_ERROR -4
#define ... 
#define ... 
Run Code Online (Sandbox Code Playgroud)

是否可以按其值打印定义的名称?像这样:

PRINT_NAME(-2);

// output
FILE_NOT_FOUND
Run Code Online (Sandbox Code Playgroud)

c macros c-preprocessor

6
推荐指数
2
解决办法
6994
查看次数

枚举结构的成员?

有没有办法枚举C ++或C中的结构(struct |类)的成员?我需要获取成员名称,类型和值。之前,我在一个小型项目中使用了以下示例代码,该项目在全局范围内。我现在遇到的问题是,需要将一组值从GUI复制到对象,文件和VM环境。我可以创建另一个“穷人”的反射系统,或者希望有一个我还没有想到的更好的东西。有人有想法吗?

编辑:我知道C ++没有反射。

union variant_t {
   unsigned int  ui;
   int           i;
   double        d;
   char*         s;
};

struct pub_values_t {
   const char*      name;
   union variant_t* addr;
   char             type;  // 'I' is int; 'U' is unsigned int; 'D' is double; 'S' is string
};

#define pub_v(n,t) #n,(union variant_t*)&n,t
struct pub_values_t pub_values[] = {
   pub_v(somemember,  'D'),
   pub_v(somemember2, 'D'),
   pub_v(somemember3, 'U'),
   ...
};

const int no_of_pub_vs = sizeof(pub_values) / sizeof(struct pub_values_t);
Run Code Online (Sandbox Code Playgroud)

c c++ enumeration

5
推荐指数
1
解决办法
4942
查看次数

使模板类型更具体(T =>某些<X>)以帮助内容辅助

如何在内部使模板类型更具体,以帮助内容辅助?

template<class T>class B{  //note: in real case, it has more template parameter
    public: void f(){}
};
template<class B1>class C{ //<-- I know for sure that B1 derived from "B<something>".    
    B1* b;
    void test(){
        b->
             ^ ctrl+space doesn't show f()
    }
};
Run Code Online (Sandbox Code Playgroud)

我糟糕的解决方法是在课堂上创建模板专业化C,但它会以另一种方式混淆内容辅助.

下面是另一种解决方法,但它非常繁琐.
我必须反映模板参数BC逐一使用这种反射.

template<class T>class B{  
    public: using reflectiveT=T;
    /* other T e.g. reflectiveT2=T2 , ... */
    public: void f(){}
};
template<class B1>class C{ 
    using BX=B<B1::reflectiveT>; //B<B1::reflectiveT1,..T2,...T3> ... tedious
    BX* …
Run Code Online (Sandbox Code Playgroud)

c++ intellisense templates visual-studio c++14

5
推荐指数
1
解决办法
258
查看次数

A variable (or an attribute) can be equal to a type?

I wanted to know if a variable can be equal to a type (here it's the magic_type)

#include <typeinfo>

template<typename T>
class C
{
public:
   magic_type t;
   t list;
   T data;

   C(void)
   {
      if (typeid(data) == typeid(int))
         t = float; // so typeid(list) = typedid(float)
      else
         t = int; // and typeid(list) = typedid(int)
   }
};
Run Code Online (Sandbox Code Playgroud)

c++ templates types class-template c++17

2
推荐指数
1
解决办法
88
查看次数

可以实例化给定类名的类吗?

是否可以在c ++中执行反射,并实例化一个给定名称为字符串的类?

干杯,

c++ visual-c++

0
推荐指数
1
解决办法
58
查看次数