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

  • Home
  • SEARCH
  • 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 8906651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:42:40+00:00 2026-06-15T02:42:40+00:00

Sample code #include <iostream> struct base {}; template<typename Type> struct left : base {

  • 0

Sample code

#include <iostream>

struct base {};

template<typename Type>
struct left : base {

   Type value;
};

template<typename Type>
struct right : base {

   Type value;
}; 

int main() {
   std::cout << "sizeof left<base> = " << sizeof(left<base>) << std::endl;
   std::cout << "sizeof left<right<base>>    = " << sizeof(left<right<base>>) << std::endl;
   std::cout << "sizeof left<right<left<right<left<base>>>>> = " << sizeof(left<right<left<right<left<base>>>>>) << std::endl;
}

Output

With GCC 4.6 is

sizeof left<base> = 2  
sizeof left<right<base>>    = 3  
sizeof left<right<left<right<left<base>>>>> = 6

With clang 3.1

sizeof left<base> = 2  
sizeof left<right<base>>    = 3  
sizeof left<right<left<right<left<base>>>>> = 6

With MSVC 2012

sizeof left<base> = 1
sizeof left<right<base>>    = 1
sizeof left<right<left<right<left<base>>>>> = 1

So, question is, is it bug in GCC/clang, or is it implementation defined, or is it right output (quotes from standard, or explanations of such behaviour will be nice)

  • 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-06-15T02:42:42+00:00Added an answer on June 15, 2026 at 2:42 am

    The relevant quote is 1.8 [intro.object] paragraph 6:

    Unless an object is a bit-field or a base class subobject of zero size, the address of that object is the address of the first byte it occupies. Two objects that are not bit-fields may have the same address if one is a subobject of the other, or if at least one is a base class subobject of zero size and they are of different types; otherwise, they shall have distinct addresses.

    In your right<T> and left<T> object (why have to different class templates? One should have been enough) you each have a member value (of type T). Each one needs to get its own unique address. Thus,

    sizeof(left<right<left<right<left<base>>>>>) == 1
    

    is definitely wrong! There are 6 distinct objects:

    • 5 values
    • one left<right<left<right<left<base>>>>>

    and only the left<right<left<right<left<base>>>>> and one of its subjects the (the first value if I recall other rules) can share an address. That is, the size of the object needs to be at least 5. Since objects work best when aligned it seems to get padded to 6 bytes (which is odd; I’d expect it to be padded to a multiple of 4).

    Even size of left<base> could’t be 1: There are two base objects involved already! One in the form of the base class of lef<base> and one in form of a member of this class. These twobase` objects need distinct addresses and, thus, the size needs to be at least 2.

    In any case, object sizes only have requirements how big they are at least. They don’t have any requirement that they shall not be bigger than something. This is considered a quality of implementation issue. Based on this, the only compiler being wrong (assuming the sizes quotes are, indeed, correct) is MSVC++. The other sizes may sometimes be slightly bigger than desired but this isn’t an error.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

consider this simple and pointless code. #include <iostream> struct A { template<int N> void
Given this sample code: #include <iostream> #include <stdexcept> class my_exception_t : std::exception { public:
I have a simple piece of c++ code: #include <iostream> using namespace std; int
Here's a sample code: #include <stack> #include <cstddef> template <std::size_t N, template <class> class
Here is the sample code that I ran on Visual Studio 2010: #include <iostream>
This very simple code: #include <iostream> using namespace std; void exec(char* option) { cout
Can anybody help me with this simple code?? #include <iostream> using namespace std; void
I have a very simple piece of code: #include <stdio.h> #include <glib.h> int main(int
Here is a code sample. // file temp.cpp #include <iostream> #include <vector> #include <boost/spirit/include/qi.hpp>
The following code: #include <boost/variant.hpp> #include <iostream> #include <string> struct A { A() {

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.