开发者

Templated classes in a union?

开发者 https://www.devze.com 2023-01-21 12:11 出处:网络
I\'m trying to get the maximum size of any possible instance of my template as a co开发者_如何学编程mpile time constant. The first thing that came to mind was something like this...

I'm trying to get the maximum size of any possible instance of my template as a co开发者_如何学编程mpile time constant. The first thing that came to mind was something like this...

union TestUnion
{
 template<typename T> class MyClass
 {
 public:
  MyClass() { };
  MyClass(T& t) : _t(t) { }

 private:
  T _t;
 };
};

But sizeof(TestUnion) is always equal to 1, but sizeof(MyClass<int>) for instance, correctly returns 4. Anyone have any better ideas?


There is no such thing as the maximum size of a template class, especially if that class contains an instance of the template argument, as yours does. Consider each of

  template <typename T> class MyClass {
    public:
      MyClass() { };
      MyClass(T& t) : _t(t) { }

    private:
      T _t;
  };

  union TestUnion {
    MyClass<char>;
    MyClass<unsigned char>;
    MyClass<signed char>;
    MyClass<short>;
    // ...
    MyClass<float>;
    MyClass<double>;
    MyClass<char*>;
    MyClass<int*>;
    // ...
    MyClass<void*>;
    MyClass<void (*)(void)>;
    MyClass<std::vector>;
    // ...
    MyClass<int[10]>;
    MyClass<int[100]>;
    MyClass<int[1000]>;
    MyClass<int[10000]>;
    MyClass<int[100000]>;
  };

and so on... Or equally excitingly, insert

    MyClass< MyClass< MyClass< ... < XImage > ... > > >

(which is admittedly not guaranteed to work at greater than the promised maximum nested template instantiation depth (17 now, 1024 soon)).

So, clearly there's no theoretical maximum. If you have a universe of types in mind that will actually be template parameters to MyClass<>, then it might be do-able.

EDIT -- replaced < with &lt; so the template argument wouldn't be hidden by the SO parser.


You are getting the size of a union that has no members. Declaring a template within a union scope doesn't declare any union members. In C++ (and also I believe C) it is guaranteed that all class or union types occupy at least 1 byte of space. So that's why the size is 1.

There is no way to get the maximum possible size, and that's because a template class can contain an instance of an arbitrary type that's not known until the template is instantiated. This type could be of arbitrary size. I will illustrate what I mean in an edit to this post within an hour or two.


I'm not an expert when it comes to sizing C++ classes... I'm sure there is a better way. Assuming you want just a potential maximum size for all the instances you plan on using, here is a likely hacky way:

const foo = (sizeof(MyClass) > sizeof(MyClass) ? (sizeof(MyClass) : sizeof(MyClass)

Then repeat to no-end nesting them as needed.

0

精彩评论

暂无评论...
验证码 换一张
取 消