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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T15:14:00+00:00 2026-06-03T15:14:00+00:00

I wonder if it is possible to initialize an entire array with a constexpr

  • 0

I wonder if it is possible to initialize an entire array with a constexpr function (with C++ 2011).
Here I have something to illustrate what I want to do :

template<unsigned int DIM> const unsigned int MyClass<DIM>::_myVar[2][3] = {
{metaFunction(0, 0, DIM), metaFunction(0, 1, DIM), metaFunction(0, 2, DIM)}, 
{metaFunction(1, 0, DIM), metaFunction(1, 1, DIM), metaFunction(1, 2, DIM)}
};

template<unsigned int DIM> inline constexpr unsigned int MyClass<DIM>::metaFunction(const unsigned int k, const unsigned int n, const unsigned int dim)
{
    return (((n < dim) && (k < n)) ? (1<<(n-k)) : (0));
}

Is there a way to initialize myVar with a constexpr without filling the array manually. And if it exists, what would be the syntax for the given example ?

To precise the question a little, I search for a way to fill all values of myVar using a single function call.

  • 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-03T15:14:03+00:00Added an answer on June 3, 2026 at 3:14 pm

    Without seeing the definition of MyClass the problem is not quite clear.
    I believe anyway that you want to get MyClass::_myVar initialized without
    code to iteratively fill it with the MyClass::metaFunction() values.

    You code suggests that MyClass::_myVar is a static class member. In that
    case your initialization of the member is perfectly good C++11 as it stands.
    The following program illustrates (GCC 4.6.3):

    #include <iostream>
    
    /* MyClass Version 1 */
    template<unsigned int DIM>
    struct MyClass
    {
        static constexpr unsigned int metaFunction(
            const unsigned int k, 
            const unsigned int n,
            const unsigned int dim);
    
        static const unsigned int _myVar[2][3]; 
    };
    
    template<unsigned int DIM> inline constexpr 
    unsigned int MyClass<DIM>::metaFunction(
        const unsigned int k, 
        const unsigned int n,
        const unsigned int dim)
    {
        return (((n < dim) && (k < n)) ? (1<<(n-k)) : (0));
    }
    
    template<unsigned int DIM> const unsigned int MyClass<DIM>::_myVar[2][3] = {
        {   metaFunction(0, 0, DIM),
            metaFunction(0, 1, DIM),
            metaFunction(0, 2, DIM)
        }, 
        {   metaFunction(1, 0, DIM),
            metaFunction(1, 1, DIM),
            metaFunction(1, 2, DIM)
        }
    };
    
    template<unsigned int DIM> inline constexpr 
    unsigned int MyClass<DIM>::metaFunction(
        const unsigned int k, 
        const unsigned int n,
        const unsigned int dim)
    {
        return (((n < dim) && (k < n)) ? (1<<(n-k)) : (0));
    }
    
    using namespace std;
    
    int main(void)
    {
        MyClass<3> mine;
        for (int i = 0; i < 2; ++i) {
            for (int j = 0; j < 3; ++j) {
                cout << mine._myVar[i][j] << endl;
            }
        }
        return 0;
    }
    

    This inclines me to think that MyClass::_myVar is not a static member –
    although why this array of integer constants would not be static I am not sure.
    If that is the case, then you can initialize the member in the default
    constructor using the uniform initialization provision of C++11:

    /* MyClass Version 2 */
    template<unsigned int DIM>
    struct MyClass
    {
        MyClass()
        :   _myVar{
                {   MyClass::metaFunction(0, 0, DIM),
                    MyClass::metaFunction(0, 1, DIM),
                    MyClass::metaFunction(0, 2, DIM)
                }, 
                {   MyClass::metaFunction(1, 0, DIM),
                    MyClass::metaFunction(1, 1, DIM),
                    MyClass::metaFunction(1, 2, DIM)
                }
        }{}
    
        static constexpr unsigned int metaFunction(
            const unsigned int k, 
            const unsigned int n,
            const unsigned int dim);
    
        const unsigned int _myVar[2][3];
    };
    

    In neither case is the constexpr attribute of metaFunction actually
    necessary for compilation. And if constexpr is removed
    then /* MyClass Version 1*/ is also good for C++03.

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

Sidebar

Related Questions

I wonder how is this possible in groovy to start an array from the
I wonder if it is possible to define a recursive function without calling the
After reading this question I started to wonder: is it possible to have a
I wonder if it possible to not have jython automagicaly transform java objects to
I wonder if its possible to have a watermark on hotlinked images on an
I'm just wonder if its possible to change States via a function in flex?
Possible Duplicate: array initialization, is referencing a previous element ok? I wonder if its
I wonder is it possible to have a map that would work like boost
I wonder if its possible to make a for loop or something similar when
I have an APK for test and I wonder if is possible to know

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.