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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T19:35:16+00:00 2026-05-15T19:35:16+00:00

I have a few classes that define sequences whose values must be available both

  • 0

I have a few classes that define sequences whose values must be available both at compile-time through a value member and at runtime as an actual instance of the type. So my base types for an arithmetic sequence looks a little like this,

template<int A, int D>
struct ArithmeticSequence : public Sequence {
    ArithmeticSequence(VALUE v)
        : Sequence(v) {}

    template<unsigned int N>
    struct VALUE_N : public VALUE {
        static const int value = A+(D*N);
        operator int() { return value; }
    };
};

class Sequence currently just defines an inner class VALUE (currently empty) and a constructor that takes a VALUE, but I will move the operator int() of VALUE_N into VALUE and Sequence will define iterators, etc, further down the line.

Now, classes should extend from ArithmeticSequence and define constants for each of the members of the sequence. I’ve got two methods that I think will work for this, if I don’t mind instances of sequences being able to be constructed from members of related sequences (that is, sequences with the same initial value and common difference), I can use typedef:

struct mySequence : public ArithmeticSequence<0,1> {
    mySequence(VALUE val = VALUE_N<0>::value)
        : ArithmeticSequence(val) {}

    typedef VALUE_N<0> zeroth;
    typedef VALUE_N<1> first;
    // ...
};

And if I do, I can extend from VALUE_N:

struct mySequence : public ArithmeticSequence<0,1> {
    mySequence(VALUE val = VALUE_N<0>::value)
        : ArithmeticSequence(val) {}

    struct zeroth : public VALUE_N<0> {};
    struct first  : public VALUE_N<1> {};
    // ...
};

In both these cases I think I can use mySequence::zeroth::value to get at the value at compile time, and mySequence::zeroth() to get a runtime object. However using the second method causes the compiler confusion as to whether I’m declaring a function or initializing an instance, so I need, mySequence s1 ((mySequence::zeroth())); instead of mySequence s1 (mySequence::zeroth()).

Now, I’ve found that the following is valid,

struct mySequence : public ArithmeticSequence<0,1> {
    mySequence(VALUE val = VALUE_N<0>::value)
        : ArithmeticSequence(val) {}

    struct zeroth : public VALUE_N<0> {};
    static const zeroth zeroth;
    struct first  : public VALUE_N<1> {};
    static const first first;
    // ...
};

But my question (finally) is, what are the rules as to which one I’m accessing at any time? I can use static const int i = mySequence::zeroth::value and, mySequence s1 (mySequence::zeroth), so the right thing seems to happen there, but if I say mySequence::zeroth z instead of treating zeroth as a class it treats it as the variable. This isn’t a problem in this case since I don’t want people creating new mySequence::zeroth‘s or any other value, but I think if I don’t understand when it will use each one I may let myself in for trouble at a later date.

Sorry about the extra long post, and thankyou in advance for your time and patience for anyone who got this far. I’m wondering now if I should have put in all the back story or just simply asked the question, if the concensus is I should have, I’ll edit it down. Thanks.

Edit. Please note, as I have written it above, using the struct method as opposed to the typedef does not provide any protection against using another “related” sequences members to construct a sequence object, it is needed, I think, however for the last example to work.

  • 1 1 Answer
  • 3 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-15T19:35:16+00:00Added an answer on May 15, 2026 at 7:35 pm

    The names of enumerators, function and objects hide the names of enumerations and classes that are declared in the same scope. In your case, the data member name hides the name of the struct. You can access the hidden type name by special lookups:

    • The name prior to :: is looked up by ignoring object-, function- and enumerator names.
    • The name used to specify a base class ignores any non-type names.
    • The name specified in an elaborated type specifier ignores object-, function- and enumerator names.

    Thus, the following elaborated type specifier is valid and refers to the class

    struct mySequence::zeroth var;
    

    Also, note that it is ill-formed when in class scope a member declaration changes the meaning of a name used in that declaration. In your case, let’s take static const first first;. The first name will refer to the type, but in the complete scope of mySequence, that name would refer to the data member. The Standard says

    A name N used in a class S shall refer to the same declaration in its context and when re-evaluated in the completed scope of S. No diagnostic is required for a violation of this rule.

    Your compiler is not required to diagnose it, which is a phrase meaning that it is effectively undefined behavior (good compilers warn you with something like “member changes meaning of name”). Albeit i doubt that the above rule is intended to apply in this case (as it is worded, it certainly applies though), you can clear the code up by using an elaborated type specifier

    struct first  : public VALUE_N<1> { };
    static const struct first first;
    

    Notice that you are required to use the elaborated type specifier in the out-of-class definition of the static member. Some compilers allow you to use the injected class name for refering to the type too (GCC did, in the past)

    const struct mySequence::first mySequence::first;
    

    The following uses the injected class name. first appears before :: and ignores the data member. But the compiler has to lookup the name mySequence::first::first to first‘s constructor and not to its class type

    const mySequence::first::first mySequence::first;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a few classes that are instantiated using ninject Session Scoping - because
I have a few classes that perform background tasks that might raise exceptions. They
I have a data access library that has a few classes that all implement
I have a few classes in a project that I inherited that are really
I have a few classes which have a property which is unique to that
I've heard before that modules are just classes too. I have a few situations,
I have few classes that I need to annotate with a name so I
Let's say I have an interface IAutoTask and few other classes implementing that interface,
I have 3 classes: an interface class that defines a few methods 2 classes
I have a few classes and I am having problems accessing properties defined in

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.