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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T16:45:36+00:00 2026-06-06T16:45:36+00:00

Here is the code for templates specilization: template <int i> struct userInput{}; template <>

  • 0

Here is the code for templates specilization:

template <int i>
struct userInput{};

template <>
struct userInput<1>
{
typedef int typeName;
};

template <>
struct userInput<2>
{
typedef double typeName;
};

And I want to choose the appropriate template according to the user input:

int i;
std::cin>>i;
userInput<i>::typeName ty;

But the compiler is not happy with me, it requires a contant value to be passed in to the template parameter.
So I did this:

int i;
std::cin>>i;    
const int p = i;
userInput<p>::typeName ty;

However, there is error :template parameter ‘i’ : ‘num’ : a local variable cannot be used as a non-type argument. Anyone can help me out? I’d appreciate it!

  • 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-06T16:45:37+00:00Added an answer on June 6, 2026 at 4:45 pm

    You will have to switch on the input to find the constant value to feed to your template.

    template <int i>
    void foo () {
        typename userInput<i>::typeName ty = 1;
        std::cout << ty/2 << std::endl;
    }
    
    int i;
    std::cin >> i;
    switch (i) {
    case 1:  foo<1>(); break;
    case 2:  foo<2>(); break;
    default: std::cerr << "invalid type: " << i << std::endl;
    }
    

    So, if the input is 1, the output is 0. If the input is 2, the output is 0.5. For others, the error message appears.

    The difference between the code in foo, and the code you tried to write is that foo<1> and foo<2> are two different functions, each with a different ty variable, and each ty variable has a different type. In contrast, your code has a single ty variable that tries to be both kinds of typeNames at the same time, sort of like Schroedinger’s Cat. The compiler failed to collapse the waveform, so it complained.

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

Sidebar

Related Questions

Here is my code: template<typename T1, typename T2> class MyClass { public: template<int num>
I have the following code: typedef vector<int> Vec; typedef vector<Vec> VecOfVec; template<typename Vec> Vec
Here is my somewhat odd code: template <typename T&> class A { public: void
Here's some code (full program follows later in the question): template <typename T> T
Having the following code: template<typename T, typename OutStream = std::ostream> struct print { OutStream
Here is a minimal code that shows the problem: template<typename To, typename From> To
I have next xsl code: <xsl:template match=/> <html> <!--some code here--> <body bgcolor=#FFFFFF> <xsl:apply-templates
Here's an example from wikipedia that displays C++ template meta-programming: template <int N> struct
I once saw this nice little snippet of code below, here at SO: template<typename
Here is a part of my html code (video urls marked with a django-template

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.