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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:42:36+00:00 2026-06-15T12:42:36+00:00

I have a mechanism like the following for retrieving certain parameters for a limited

  • 0

I have a mechanism like the following for retrieving certain parameters for a limited number of different types of objects I need to handle:

template <class T>
struct params {};

template <>
struct params<FooObj>
{
    static const int paramA = 17;
    static const int paramB = 29;
};

this simplifies my code later because in a switch statement when I handle treating different objects, if I get a FooObj then all I have to do is something like this:

typedef params<FooObj> paramsT;

and then in that code snippet I have access to parameters to work with that FooObj via paramsT::paramC or whatever.

Now I have run into an object where I have something like this:

template <>
struct params<BarObj>
{
    static const int paramA  = 0;
    static const int paramB  = 9;
    static const int paramC  = 17;
    static const int paramD1 = 18;
    static const int paramE1 = 20;
    static const int paramD2 = 28;
    static const int paramE2 = 30;
    static const int paramD3 = 38;
    static const int paramE3 = 40;
    static const int paramD4 = 48;
    static const int paramE4 = 50;
    static const int paramD5 = 58;
    static const int paramE5 = 60;
    static const int paramD6 = 68;
    static const int paramE6 = 70;
};

and when I’m handling this object I started writing something like the following:

typedef params<BarObj> paramsT;
BarObj bar;
//load the first 3 params via the above info into bar

int a,b;
for (int i = 1; i <= 6; ++i)
{
    a = doSomethingA(bla + paramsT::paramD1);
    b = doSomethingB(bla + paramsT::paramE1);
    bla.paramD1 = functionOf(stuff,and,a,b);
}

but of course the above has 1 hardcoded into it, and it would ideally read something like this:

typedef params<BarObj> paramsT;
BarObj bar;
//load the first 3 params via the above info into bar

int a,b;
for (int i = 0; i < 6; ++i)
{
    a = doSomethingA(bla + paramsT::paramD[i]);
    b = doSomethingB(bla + paramsT::paramE[i]);
    bla.paramD[i] = functionOf(stuff,and,a,b);
}

although for something like the above I would need the params template specialization to be something like this:

template <>
struct params<BarObj>
{
    static const int paramA   = 0;
    static const int paramB   = 9;
    static const int paramC   = 17;
    static const int paramD[] = {18, etc..};
    static const int paramE[] = {20, etc..};
};

which doesn’t compile because the arrays even though hardcoded are non-integral types. Is there an easy patch around this that wouln’t hopefully look too different from my current usage? Or a way to get that array stuff in 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-15T12:42:38+00:00Added an answer on June 15, 2026 at 12:42 pm

    The “static implicitly inline function static local variable” hack:

    template<typename T>
    struct params;
    struct Bob;
    template<>
    struct params<Bob> {
      static int paramE(unsigned int idx) {
        static const int v[] = {18, 20, 22, 24};
        return v[idx];
      }
    };
    
    #include <iostream>
    int main() {
      for(auto i = 0; i < 4; ++i)
        std::cout << params<Bob>::paramE(i) << "\n";
    }
    

    note that the resulting values are not “compile time constants” (ie, cannot be used for things like template parameters), but are trivial for compilers to optimize into constants.

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

Sidebar

Related Questions

I have the following use case: I need to process a big number of
I have a test sample about coherence lock-unlock mechanism like this: public class Test
I'd like to know if there are any mechanism in Akka that can have
I have something that looks like the following document structure: public class Document {
I'm experiencing some troubles with Qt's signals and slots mechanism. I have something like
I have the following locking mechanism for my class. When I run this program,
Say I have the following classes: template <class T> class Base { protected: T
We have a mechanism that lets a user view available Accelerators (attachments) for a
I have a voting mechanism where you can vote up/down on an entry within
I seem to remember that Regular Expressions in DotNet have a special mechanism that

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.