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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T11:01:33+00:00 2026-06-16T11:01:33+00:00

I have a template: template<unsigned int N> struct IntN; template <> struct IntN< 8>

  • 0

I have a template:

template<unsigned int N> struct IntN;

template <> struct IntN< 8> {
   typedef uint8_t type;
}; 

template <> struct IntN<16> {
   typedef uint16_t type;
};

And in main I initialise and alternate by doing this:

IntN< 8>::type c;

This seems to work, however, when I store the value inside a variable, it does not and I get the following error:

error: non-type template argument of type ‘int’ is not an integral constant expression

Here is an example of the code:

template<unsigned int N> struct IntN;

template <> struct IntN< 8> {
  typedef uint8_t type;
};

template <> struct IntN<16> {
   typedef uint16_t type;
};

int main(int argc, char *argv[]) {
int foo = 8;

IntN<foo>::type c;
}

Does anyone have any ideas? Thanks

  • 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-16T11:01:34+00:00Added an answer on June 16, 2026 at 11:01 am

    The template arguments for integral template parameters must be constant expressions. An integral literal is a constant expression

    IntN<8>::type c;
    

    A constant variable initialized with a constant expression is a constant expression

    const int n = 8;
    IntN<n>::type c;
    

    Here n is OK because it’s both const and initialized by a constant expression (8). The following will not compile though:

    int n = 8;
    const int m = n;
    IntN<n>::type c; //error n is not const therefore not a constant expression
    IntN<m>::type c; //error m is const but initialized with a non-constant expression therefore not a constant expression
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to do something like this: template<typename CType, unsigned int targetDimensions> struct GeneratePointerType
I have the following template structure: template <typename scalar_type> struct postc_params{ scalar_type delta; unsigned
I have a class like this: template <class T> class bag { private: typedef
I have the following class: template<unsigned int offsetX, unsigned int offsetY> class myClass {
Possible Duplicate: What does template <unsigned int N> mean? Hi ! Are non-type template
I have a struct wich contains: struct mystruct{ int id[10]; char text[40]; unsigned short
I have a class template <unsigned int N> class StaticVector { // stuff };
I have this class to factorize common operations on N-dimensional vector spaces: template <unsigned
I have some code that looks like: template<unsigned int A, unsigned int B> int
I have the following class: #include <array> template<unsigned short D> class Point { private:

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.