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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:29:46+00:00 2026-05-27T00:29:46+00:00

I have the following code: template<int k> void foo() { } int main(int argc,

  • 0

I have the following code:

template<int k>
void foo()
{
}
int main(int argc, char* argv[])
{
    int k = 1000;
    foo<k>();
    return 0;
}

which doesn’t compile, but if I declare k as const, it does:

template<int k>
void foo()
{
}
int main(int argc, char* argv[])
{
    const int k = 1000;
    foo<k>();
    return 0;
}

Now, I see the logic behind why in the first case it doesn’t compile and in the second it does, but is this specified by the standard?

The error I’m getting is:

Error   1   error C2971: 'foo' : template parameter 'k' : 'k' : a local variable cannot be used as a non-type argument

which isn’t exactly clear, since k is a local variable also in the case it’s const… right?

  • 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-05-27T00:29:47+00:00Added an answer on May 27, 2026 at 12:29 am

    §14.3.2.1 says [abridged]:

    A template-argument for a non-type, non-template template-parameter shall be one of:
    — an integral constant-expression of integral or enumeration type;

    And §5.19.1 says [abridged, emphasis mine]:

    An integral constant-expression can involve only literals, enumerators, const variables or static data members of integral or enumeration types initialized with constant expressions…

    Your second definition of k satisfies this, so it’s allowed to be used as an ICE for the template argument.

    The error is slightly misleading in that “a local variable cannot be used as a non-type argument” is true in general, but with certain restrictions it’s perfectly fine.

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

Sidebar

Related Questions

I have the following code: int main(void) { struct { int x; } a,
I have the following code: template <typename Provider> inline void use() { typedef Provider::Data<int>
I'm using gcc 4.3.2. I have the following code (simplified): #include <cstdlib> template<int SIZE>
I have the following piece of code which replaces template markers such as %POST_TITLE%
I have the following source code : // main.cpp #include a.h int main() {
I have the following piece of code which doesn't compile when I try to
The following code, which attempts to specialize class template 'special', based on the return
I have the following C code: static void* heap; static unsigned int ptr; int
while compiling the following code i have got error @Override public void routeCustomerRequest (int
I have the following code: template <typename M> struct Matrix { Matrix(int size);//(int x

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.