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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:33:35+00:00 2026-06-05T08:33:35+00:00

I seem to be missing something rather fundamental. I’m trying to use const array

  • 0

I seem to be missing something rather fundamental.
I’m trying to use const array members at compile-time.

const int list[3] = { 2, 5, 7 };
const int a = list[2]; // this doesn't error?

template<int N1, int N2>
struct tmax {
  enum { value = ((N1 > N2) ? N1 : N2) };
};

const int b = tmax<2,4>::value;
const int c = tmax<list[0],list[1]>::value; // error is here

int main()
{
  return 0;
}

Errors:

prog.cpp:10:24: error: 'list' cannot appear in a constant-expression
prog.cpp:10:30: error: an array reference cannot appear in a constant-expression

Here is the relevent IDEOne link

So why doesn’t this work? What am I missing? What should I do differently?

  • 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-05T08:33:36+00:00Added an answer on June 5, 2026 at 8:33 am

    Just because an object is const doesn’t mean it’s a compile time constant expression.

    main.cpp:10:20: error: non-type template argument is not a constant expression
    const int c = tmax<list[0],list[1]>::value; // error is here
                       ^~~~~~~
    main.cpp:10:20: note: read of non-constexpr variable 'list' is not allowed in a constant expression
    main.cpp:1:11: note: declared here
    const int list[3] = { 2, 5, 7 };
              ^
    

    This is the reason for constexpr:

    constexpr int list[3] = { 2, 5, 7 };
    
    template<int N1, int N2>
    struct tmax {
        enum { value = ((N1 > N2) ? N1 : N2) };
    };
    
    const int b = tmax<2,4>::value;
    const int c = tmax<list[0],list[1]>::value; // works fine now
    

    As for why this works:

    const int a = list[2]; // this doesn't error?
    

    initializing a const variable doesn’t require a constant expression:

    int foo(int n) {
        const int a = n; // initializing const var with a non-compile time constant
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Maybe I am missing something, but they seem similar. If you use for example...
I seem to be missing something silly here. int main() { /* bitset<sizeof(int)*8> bisetInt(64);
I seem to be missing something quite fundamental here and yet my code doesn't
I'm trying to learn java and seem to be missing something obvious. In subdirectory
I seem to be missing something obvious here, why would I want more than
Maybe I'm just missing something in the docs, but it seem it's not possible
I know I am probably missing something, but I can't seem to get windows
I'm really new to SVN and seem to be missing something here... I have
I might be missing something obvious here, but when trying to expose a Q_ENUM
I seem to be missing something very simple. All i want to do is

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.