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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:13:47+00:00 2026-06-11T12:13:47+00:00

Say I have the following example: #include <cstdlib> class A { public: static const

  • 0

Say I have the following example:

#include <cstdlib>

class A {
public:
    static const std::size_t value = 42;
};

In short, I have (or better, want) a class A with a static const std::size_t member called value with the value 42 (determined at compile time).

Now, IIRC, this only works fine under certain circumstances. It doesn’t, for example, when you take the address of A::value. In order for this to work fine in all cases you’d need to add a definition in some implementation file:

const std::size_t A::value;

However, I can’t do this, because I want this file to be header-only. Another common solution is this:

class A {
public:
    enum { value = 42 };
};

I don’t like this solution either, because I’d like the type of A::value to be std::size_t.

What is a good solution to this problem? Preferably a small and portable solution, not something with huge macro magic like BOOST_STATIC_CONSTANT.


I’d like a solution for C++03, not C++11 (it’s trivial there).

  • 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-11T12:13:48+00:00Added an answer on June 11, 2026 at 12:13 pm

    First of all, using the unsigned size_t type for numbers, you’re likely to run into implicit promotion problems. So, good idea to use its corresponding signed type, which is called ptrdiff_t. Which, as it happens, is the result type of a pointer difference expression.

    Also, due to changes in C++11, it’s generally a good idea to include <stddef.h> and not <cstddef>, i.e., write ::ptrdiff_t or just plain ptrdiff_t, not std::ptrdiff_t.

    Now, here’s how to do the header file extern linkage constant thing:

    template< class Dummy >
    struct A_constants_
    {
        static ::ptrdiff_t const value;
    };
    
    template< class Dummy >
    ::ptrdiff_t const A_constants_<Dummy>::value = 42;
    
    typedef A_constants_<void> A_constants;
    
    class A
        : public A_constants
    {
    public:
        // Whatever
    };
    

    Then you can use it like this:

    foo( A::value );
    

    There are also some other ways of doing this, but the above is about the simplest and easiest to get right.

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

Sidebar

Related Questions

Say suppose I have the following Java code. public class Example { public static
Say I have a class, something like the following; class MyClass { public: MyClass();
Say for example, I have the following: public interface IPet { long? Id {
I do not understand the following example, let's say I have these functions: #
Say I have the following code: <div onclick='location.href=http://www.example.com/'> <a href='#' onclick='alert(blah)'>click</a> </div> Is there
For example, let's say I have the following two tables: Table1: Id ---- 1
Say for example I have the following styles: #HorizNav ul li a.active:link { background-color:
Say for example, I have the following two tables: TableA { _id } TableB
Lets say we have following models. class User(db.Model): username=db.StringProperty() avatar=db.ReferenceProperty() class User(db.Model): username=db.StringProperty() avatar=db.StringProperty()
Let's say I have following table like this <!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0

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.