11

Possible Duplicate:
Is it possible to print out the size of a C++ class at compile-time?

Can I output the size of an object at compile time? Since the compiler already has this information when it is compiling a source file, can I see it (at compile time) rather than going through the lengthy process of outputting the size somewhere in my application's console or the debug output window?

This will be very useful especially when I am able to compile single source files which saves me tremendous amounts of time when working on large projects.

Community
  • 1
  • 1
Samaursa
  • 16,527
  • 21
  • 89
  • 160
  • Is this what you're looking for? http://stackoverflow.com/questions/2008398/is-it-possible-to-print-out-the-size-of-a-c-class-at-compile-time – wkl Oct 28 '11 at 15:27
  • Many compilers have a `#pragma` or other directive that can be used to output values during compilation. You'll have to check your compiler's documentation. Unfortunately I don't know how to get the compiler to evaluate the expression before it generates the output. – Mark Ransom Oct 28 '11 at 15:30
  • 1
    I'm not answering, because I'm no entirely sure, but I think the answer is no. – Michael Krelin - hacker Oct 28 '11 at 15:31
  • @MichaelKrelin-hacker, why do you think this is a different question? It looks exactly the same to me. – Mark Ransom Oct 28 '11 at 15:34
  • @MarkRansom, there were two links, I believe and, probably, I was referring to the other wan, that was about determining the size, not displaying it. Unless I went crazy since my last comment. – Michael Krelin - hacker Oct 28 '11 at 15:36
  • Definitely a duplicate, but the question's search terms did not match at all. – Samaursa Oct 28 '11 at 15:40
  • 2
    Samaursa, don't feel bad about not finding it through search and don't take the closing personally. It's a good thing to have different questions with different search terms, but it's also good to have all the answers in one place. – Mark Ransom Oct 28 '11 at 18:33

1 Answers1

21

Yes. The possible duplicate prints the size as error message, which means the compilation will not succeed.

However, my solution prints the size as warning message, which means, it will print the size, and the compilation will continue.

template<int N> 
struct print_size_as_warning
{ 
   char operator()() { return N + 256; } //deliberately causing overflow
};

int main() {
        print_size_as_warning<sizeof(int)>()();
        return 0;
}

Warning message:

prog.cpp: In member function ‘char print_size_as_warning<N>::operator()() [with int N = 4]’:
prog.cpp:8:   instantiated from here
prog.cpp:4: warning: overflow in implicit constant conversion

Demo : http://www.ideone.com/m9eg3

Note : the value of N in the warning message is the value of sizeof(int)


The above code is improved one, and my first attempt was this:

template<int N> 
struct _{ operator char() { return N+ 256; } }; //always overflow

int main() {
        char(_<sizeof(int)>());
        return 0;
}

Warning message:

prog.cpp: In member function ‘_<N>::operator char() [with int N = 4]’:
prog.cpp:5:   instantiated from here
prog.cpp:2: warning: overflow in implicit constant conversion

Demo : http://www.ideone.com/mhXjU

The idea is taken from my previous answer to this question:

Community
  • 1
  • 1
Nawaz
  • 353,942
  • 115
  • 666
  • 851