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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:08:16+00:00 2026-05-20T18:08:16+00:00

I have wrote the following code to get the offset of a tuple element

  • 0

I have wrote the following code to get the offset of a tuple element

template<size_t Idx,class T>
 constexpr size_t tuple_element_offset() {
        return static_cast<size_t>(
                    reinterpret_cast<char*>(&std::get<Idx>(*reinterpret_cast<T*>(0))) - reinterpret_cast<char*>(0));
    }

This is actually similar to the implementation of the offsetof macro.
It looks ugly, but compiles and works fine on gcc-4.6

typedef std::tuple<int,char,long> mytuple;

mytuple var = std::make_tuple(4,'c',1000);
char * ptr = reinterpret_cast<char*>(&var);
long * pt = reinterpret_cast<long*>(ptr+tuple_element_offset<2,mytuple>());

std::cout << *pt << std::endl;

prints “1000”.

I don’t know too much about constexpr, so my questions are:

  1. Is it legal c++?
  2. More important, why I am allowed to call
    std::get (which is non constexpr)
    inside a constexpr function?

As far as I understand constexpr, the compiler is forced to evaluate the result
of the expression at compile time, so no zero-dereferentiation can occurs in practice.

  • 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-20T18:08:16+00:00Added an answer on May 20, 2026 at 6:08 pm

    Is it legal C++?

    If by “legal” you mean “well-formed,” then, yes.

    If by “legal” you mean “valid and will work on any compiler and Standard Library implementation, then, no, because std::tuple is not POD.

    Why I am allowed to call std::get (which is not constexpr) inside a constexpr function?

    Basically, a constexpr function doesn’t necessarily have to consist of just a constant expression. If you tried to use your tuple_element_offset() function in a constant expression, you’d get a compilation error.

    The idea is that a function might be usable in a constant expression in some circumstances but not in others, so there isn’t a restriction that a constexpr function must always be usable in a constant expression (since there isn’t such a restriction, it’s also possible that a particular constexpr function might never be usable in a constant expression, as is the case with your function).

    The C++0x draft has a good example (from 5.19/2):

    constexpr const int* addr(const int& ir) { return &ir; } // OK
    
    // OK: (const int*)&(const int&)x is an address contant expression
    static const int x = 5;
    constexpr const int* xp = addr(x); 
    
    // Error, initializer for constexpr variable not a constant expression; 
    // (const int*)&(const int&)5 is not a constant expression because it takes
    // the address of a temporary
    constexpr const int* tp = addr(5);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: MemoryStream foo(){ MemoryStream ms = new MemoryStream(); // write
I wrote this code I have these errors Cannot implicitly convert type x.Program.TreeNode' to
i have the following code trying to save the contents of a JTextPane as
I wrote the following code under the assumption that Excel will die with Monkey:
hi am working with NSArrays using foundation tool and i have wrote the following
I wrote the following code in python self._job = None #slider def sliderCallback(self): if
I want to get class methods in an object. Please see the following example
I have the following code: $('#main-nav a').mouseover(function() { var name = $(this).attr(rel); $(#subnav ul.
Is there a memory leak in the following code example as I have allocated
I have the following projects: MVC Console application Class library Windows forms application COM

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.