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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:52:10+00:00 2026-06-12T14:52:10+00:00

I have the need to use offsetof from a template with a member selector.

  • 0

I have the need to use offsetof from a template with a member selector. I’ve come up with a way, if you’ll excuse the awkward syntax:

template <typename T,
          typename R,
          R T::*M
         >
constexpr std::size_t offset_of()
{
    return reinterpret_cast<std::size_t>(&(((T*)0)->*M));
};

Usage isn’t perfect (annoying at best):

struct S
{
    int x;
    int y;
};

static_assert(offset_of<S, int, &S::x>() == 0, "");
static_assert(offset_of<S, int, &S::y>() == sizeof(int), "");

The non-constexpr form is easier to use:

template <typename T, typename R>
std::size_t offset_of(R T::*M)
{
    return reinterpret_cast<std::size_t>(&(((T*)0)->*M));
};

at the obvious disadvantage that it isn’t done at compile-time (but easier to use):

int main()
{
    std::cout << offset_of(&S::x) << std::endl;
    std::cout << offset_of(&S::y) << std::endl;
}

What I’m looking for is syntax like the non-constexpr variety, but still fully compile-time; however, I can’t come up with the syntax for it. I would also be happy with an offset_of<&S::x>::value (like the rest of the type traits), but can’t figure out the syntax magic for it.

  • 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-12T14:52:11+00:00Added an answer on June 12, 2026 at 2:52 pm

    The following should work (credits go to the answer to this question for the idea):

    #include <cstddef>
    
    template <typename T, typename M> M get_member_type(M T::*);
    template <typename T, typename M> T get_class_type(M T::*);
    
    template <typename T,
              typename R,
              R T::*M
             >
    constexpr std::size_t offset_of()
    {
        return reinterpret_cast<std::size_t>(&(((T*)0)->*M));
    }
    
    #define OFFSET_OF(m) offset_of<decltype(get_class_type(m)), \
                         decltype(get_member_type(m)), m>()
    
    struct S
    {
        int x;
        int y;
    };
    
    static_assert(OFFSET_OF(&S::x) == 0, "");
    

    Note that in gcc, the offsetof macro expands to a builtin extension which can be used at compile time (see below). Also, your code invokes UB, it dereferences a null pointer, so even if it might work in practice, there are no guarantees.

    #define offsetof(TYPE, MEMBER) __builtin_offsetof (TYPE, MEMBER)
    

    As pointed out by Luc Danton, constant expressions cannot involve a reinterpret_cast according to the C++11 standard although currently gcc accepts the code (see the bug report here). Also, I found defect report 1384 which
    talks about making the rules less strict, so this might change in the future.

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

Sidebar

Related Questions

I have a xsl file where i need to use parameters from an external
I have two tables (Master, Responses) I need use the Master_Id field from the
I have the need to use a Stack-like data structure for a program that
I have installed Matlab r2010a on my computer I need use the function xlsread
I need to use IBM Informix for my project where I have point coordinates
I need to use something like get_or_create() but the problem is that I have
I have some files that need to use by my application when it is
I need to use the mongo-storm library and Clojars doesn't have it . So
Need to use own imaged markers instead built-in pins. I have several questions. 1.
I have a timed page that I need to use to submit a form

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.