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 6631823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:36:02+00:00 2026-05-25T22:36:02+00:00

In C++0x, I would like to determine if a class is trivial/has standard layout

  • 0

In C++0x, I would like to determine if a class is trivial/has standard layout so I can use memcpy(), memset(), etc…

How should I implement the code below, using type_traits, so I can confirm that a type is trivial?

template< typename T >
bool isTrivialType()
{
  bool isTrivial = ???
  return isTrivial;
}

NOTE: is_pod() is too restrictive: I would like my class to have trivial constructors, etc… …for convenience.

Added: I think std::is_standard_layout<> may give me what I’m looking for.
1. If I add constructors, it still returns true
2. If I add a virtual method, it returns false
This is what I need to determine if I can use memcpy(), memset()

Edit: From Luc Danton’s explanation and link below (clarification):

struct N { // neither trivial nor standard-layout
   int i;
   int j;
    virtual ~N();
};

struct T { // trivial but not standard-layout
    int i;
private:
    int j;
};

struct SL { // standard-layout but not trivial
    int i;
    int j;
    ~SL();
};

struct POD { // both trivial and standard-layout
    int i;
    int j;
};

For memcpy() to be happy:

// N -> false
// T -> true
// SL -> ??? (if there are pointer members in destructor, we are in trouble)
// POD -> true

So it does look like is_trivial_class<> is correct: is_standard_layout<> is not necessarily right…

  • 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-25T22:36:03+00:00Added an answer on May 25, 2026 at 10:36 pm

    For std::memcpy it is sufficient that the type be trivially copyable. From n3290, 3.9 Types [basic.types] paragraph 2:

    For any object (other than a base-class subobject) of trivially copyable type T, whether or not the object holds a valid value of type T, the underlying bytes (1.7) making up the object can be copied into an array of char or unsigned char.

    Following paragraphs also describe other useful properties of trivially copyables types (i.e. not just copying to a char array).

    std::is_trivially_copyable is the trait to detect just that. However as of my writing it’s not implemented by e.g. GCC, so you may want to use std::is_trivial as a fallback (since in turn it requires a trivial copy constructor).

    I really do not recommend using is_standard_layout, unless you really know what you’re doing (e.g. language interoperability on one particular platform) it’s not what you want. More information on what triviality and standard layout is about to perhaps help you specify the exact requirements you want.

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

Sidebar

Related Questions

I would like to determine at compile time if a pointer to Derived can
I have a template variable, c.is_friend, that I would like to use to determine
I am working on a class which I would like to use to log
I have a configuration class that I would like to use for a variety
I would like to programmatically determine all properties of a Scala class at runtime.
I would like to determine what the long url of a short url is.
I would like to determine a remote page's encoding through detection of the Content-Type
I have two times in PHP and I would like to determine the elapsed
Let's say I have the string Hey . I would like to determine all
I'm currently writing a program in Python, and I would like to determine the

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.