0

I need something that will work with the following:

struct thing *s;
printf("the size of %s is %d", s->type, (int) sizeof(s->type));

I thought I had at last found a solution with typedef, but no:

struct thing {
  typedef int type;
}

expects syntax of the form thing::type. I suspect the solution still involves typedef, but I need the type to actually be a member, as opposed to a nested type.

Edit: Whoops, looks like the functionality I needed was just a string after all. Sorry, thanks for helping!

4

1 回答 1

1

我认为这可能是您目前可以做的最接近的事情,尽管类型名称可能会被破坏(尚未找到尚未找到的实现):

struct thing {
  int i;
};

thing t;
std::cout << "the size of " << typeid(t.i).name() 
          << " is " << sizeof(t.i) << "\n";
于 2013-10-08T21:14:58.037 回答