Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6765541
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:43:26+00:00 2026-05-26T14:43:26+00:00

Possible Duplicate: Is it possible to print out the size of a C++ class

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T14:43:26+00:00Added an answer on May 26, 2026 at 2:43 pm

    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:

    • Calculating and printing factorial at compile time in C++
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How to print out the method name and line number and conditionally
Possible Duplicate: File Upload ASP.NET MVC 3.0 I am trying to just print out
Possible Duplicate: nul terminating a int array I'm trying to print out all elements
Possible Duplicate: Float to binary in C++ I want to print out the binary
Possible duplicate: Print regex matches in java I am using Matcher class in java
Possible Duplicate: C++: Print out enum value as text Say I have an enumeration:
Possible Duplicate: Weird Java Boxing Hi, Can somebody explain why the last print returns
Possible Duplicate: Why do I see strange values when I print uninitialized variables? Fun
Possible Duplicate: C compiler bug (floating point arithmetic)? I've got two doubles which I
Possible Duplicate: C/C++ line number Hi, I have a simple error manager class that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.