Re: how to compare the sizes of 2 structures at compile time

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Victor Bazarov (v.Abazarov_at_comAcast.net)
Date: 07/09/04


Date: Fri, 09 Jul 2004 14:38:01 -0400

paul@slowmind.com wrote:
> I have 2 structures _test1 and _test2. At compile time, I want the compiler to issue an error if
> sizeof( _test1 ) != sizeof( _test2)

You can probably do it using templates:

   template<bool trueorfalse> struct If;
   template<> struct If<true> { enum { satisfied }; };
   template<> struct If<false> { enum { notsatisfied }; };

   int a, b;
   double c, d;

   int main()
   {
     If<sizeof(a) == sizeof(b)>::satisfied;
     If<sizeof(c) == sizeof(a)>::satisfied; // will get "satisfied" is not
                                            // a member" error message

     return 0;
   }

Should work for structs just as well.

Victor



Relevant Pages

  • Re: Win AP question
    ... This means that you can't just pass an array of structs. ... memory which is large enough for the structs and the string data. ... public static extern int EnumJobs( ... > public string pPrinterName; ...
    (microsoft.public.dotnet.languages.csharp)
  • Beware CS1612 when dealing with "Point" (its not really a structure like most of us think of)
    ... modify a Point, which I had made have a property. ... like 'int' or 'nullable' types, and does not really have 'member ... allow you to return a reference to a value type. ... The only issue I have with the way that structs are implemented ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Is Class Synonymous with Type?
    ... So-called "Native Types" (int, double, struct, enum, etc) ARE classes, which ... Similarly Structs are not classes. ... that encapsulate the native types. ... The above declare three variables in C of type int, float and pointer to ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: valid types for Keys (Hashtable, Dictionary)
    ... structs have very specific applications in .NET, ... just like an int or a double. ... public override int GetHashCode() ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Recursive Function
    ... recursive function (and a cursor), ... insert into Victor values ... ord int identity, ... ParentPageID int, ...
    (microsoft.public.sqlserver.server)