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

  • Home
  • SEARCH
  • 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 8796775
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:42:01+00:00 2026-06-13T23:42:01+00:00

In a project, I have several classes which encapsulate, among other things, matrices implemented

  • 0

In a project, I have several classes which encapsulate, among other things, matrices implemented as static-arrays, e.g.:

struct StaticContainer
{
    static const short e[2][4];
};

/// Initialize components of member-array (with external linkage).
const short StaticContainer::e[2][4] = {
    { -1,  0, 0, 1 },
    {  0, -1, 1, 0 }
};

I would like to implement a meta-function which provides the inverse-mapping, from a column in StaticContainer::e back to the second index (1-4 in this case). Ideally, something like this:

template< typename TContainer, short TeX, short TeY >
struct VectorToIndex
{
    enum { value = ??? };
};

Finally, I would like to pass (if this is possible at all):

BOOST_STATIC_ASSERT( 0 == VectorToIndex< StaticContainer, -1, 0 >::value );

Is this possible at all? My initial attempts to recursively-search through the ‘e’-matrix failed, because whenever I try to access (at compile-time) the entries within I get (GCC):

error: ‘StaticContainer::e’ cannot appear in a constant-expression

Shall I understand that the values in the matrix are not available at compile-time?

I would appreciate any comments. I am free to change the way the matrix is initialized/stored (so I was thinking of some compile-time registration mechanism). The only constraint is to get this inverse-mapping at compile-time.

Clarifications:

  • Each column in the e-matrix represents a spatial direction (in this case, 2D). The columns are guaranteed to be distinct.

  • I would expect the following results from the meta-function:

    VectorToIndex< StaticContainer, -1, 0 > --> '0' at compile-time
    VectorToIndex< StaticContainer,  0,-1 > --> '1' at compile-time
    VectorToIndex< StaticContainer,  0, 1 > --> '2' at compile-time
    VectorToIndex< StaticContainer,  1, 0 > --> '3' at compile-time
    

If this template is instantiated with an invalid combination of numbers (i.e. which is not a column in the matrix), I would like to produce a compilation-error.

  • The solution I currently have is a simple program which writes files with the necessary template-instantiations manually. This satisfies the requirements (results are correct and for invalid vectors there is a compile-time error – since the corresponding template-instantiation is missing). However, since I have many classes similar to ‘StaticContainer’ in my codebase (many of them with larger matrices), this process generates thousands of lines of code :(.
  • 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-13T23:42:02+00:00Added an answer on June 13, 2026 at 11:42 pm

    As promissed, here goes a solution. Rather than reinventing a whole metaprogramming library using variadic templates, I used this opportunity to try out boost mpl and it turns out to be pretty expressive. Using it, VectorToIndex would look like the following:

    template<typename basis, typename axis>
    struct VectorToIndex : 
        boost::mpl::if_<
            boost::mpl::greater<boost::mpl::size<basis>, typename boost::mpl::find<basis, axis>::type::pos>,
            typename boost::mpl::find<basis, axis>::type::pos,
            boost::mpl::empty_base
        >::type
    {
    };
    

    If “axis” is present in “basis” then VectorToIndex<basis, axis>::value equals its index in the range [0, size-of-basis). If otherwise this is not true, than VectorToIndex<basis, axis>::value is not defined, thus accessing it may be used to produce compile-time errors or
    selective instanciation through SFINAE.

    To represent the axis one should use boost::mpl::vector, like shown below:

    typedef boost::mpl::vector<boost::mpl::int_<1>, boost::mpl::int_<0>, boost::mpl::int_<0> > e1;
    typedef boost::mpl::vector<boost::mpl::int_<0>, boost::mpl::int_<1>, boost::mpl::int_<0> > e2;
    typedef boost::mpl::vector<boost::mpl::int_<0>, boost::mpl::int_<0>, boost::mpl::int_<1> > e3;
    

    Considering the definitions above, one has

    VectorToIndex<boost::mpl::vector<e1, e2, e3>, e1>::value -> 0
    VectorToIndex<boost::mpl::vector<e1, e2, e3>, e2>::value -> 1
    VectorToIndex<boost::mpl::vector<e1, e2, e3>, e3>::value -> 2
    VectorToIndex<boost::mpl::vector<e1, e2>, e3>::value -> COMPILE-TIME ERROR
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a project in C# 4.0. I have several presenter classes, which
I have several classes in a project which need to only be in certain
MVC3 project. I have several classes: Account, Address, Phone etc. which I set up
This is for a web project so i have several classes that inherit from
I have some data/helper classes defined which I use in several projects. These data
I have a project called MyApp.DataAccess which contains several database contexts. This started as
I am coding a desktop project which will includes several classes, and will put
I have several maps containing a multitude of classes in my VC++ project, some
I have inherited a Java project which includes a number of classes that are
In our project, we're using gtkmm and we have several classes that extend Gtk::Window

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.