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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T18:45:44+00:00 2026-05-12T18:45:44+00:00

A common question that comes up from time to time in the world of

  • 0

A common question that comes up from time to time in the world of C++ programming is compile-time determination of endianness. Usually this is done with barely portable #ifdefs. But does the C++11 constexpr keyword along with template specialization offer us a better solution to this?

Would it be legal C++11 to do something like:

constexpr bool little_endian()
{
   const static unsigned num = 0xAABBCCDD;
   return reinterpret_cast<const unsigned char*> (&num)[0] == 0xDD;
}

And then specialize a template for both endian types:

template <bool LittleEndian>
struct Foo 
{
  // .... specialization for little endian
};

template <>
struct Foo<false>
{
  // .... specialization for big endian
};

And then do:

Foo<little_endian()>::do_something();
  • 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-12T18:45:44+00:00Added an answer on May 12, 2026 at 6:45 pm

    Assuming N2116 is the wording that gets incorporated, then your example is ill-formed (notice that there is no concept of “legal/illegal” in C++). The proposed text for [decl.constexpr]/3 says

    • its function-body shall be a compound-statement of the form
      { return expression; }
      where expression is a potential constant expression (5.19);

    Your function violates the requirement in that it also declares a local variable.

    Edit: This restriction could be overcome by moving num outside of the function. The function still wouldn’t be well-formed, then, because expression needs to be a potential constant expression, which is defined as

    An expression is a potential constant expression if it is a constant
    expression when all occurrences of function parameters are replaced
    by arbitrary constant expressions of the appropriate type.

    IOW, reinterpret_cast<const unsigned char*> (&num)[0] == 0xDD would have to be a constant expression. However, it is not: &num would be a address constant-expression (5.19/4). Accessing the value of such a pointer is, however, not allowed for a constant expression:

    The subscripting operator [] and the class member access . and
    operators, the & and * unary operators, and pointer casts (except dynamic_casts, 5.2.7) can be used in the creation of an
    address constant expression, but the value of an object shall not be accessed by the use of these operators.

    Edit: The above text is from C++98. Apparently, C++0x is more permissive what it allows for constant expressions. The expression involves an lvalue-to-rvalue conversion of the array reference, which is banned from constant expressions unless

    it is applied to an lvalue of effective integral type that refers
    to a non-volatile const variable or static data member initialized
    with constant expressions

    It’s not clear to me whether (&num)[0] “refers to” a const variable, or whether only a literal num “refers to” such a variable. If (&num)[0] refers to that variable, it is then unclear whether reinterpret_cast<const unsigned char*> (&num)[0] still “refers to” num.

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

Sidebar

Related Questions

This question comes from a range of other questions that all deal with essentially
As the question says, what are some common/major issues that C++ programmers face when
Quick question about include/requre_once . I have some code that is common to a
This is a common question, but the explanations found so far and observed behaviour
I think this is a fairly common question: how to put my business logic
I know this is a common question and there are oodles of resources on
I've searched all over and couldn't find an answer to this seemingly common question,
This is a generic C++ design question. I'm writing an application that uses a
This is a rather broad question, but I'm a beginner-intermediate programmer that has just
This code is taken from a previous question, but my question directly relates to

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.