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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:22:54+00:00 2026-05-27T22:22:54+00:00

i am trying to learn templates in c++. Can someone please explain why sq()

  • 0

i am trying to learn templates in c++. Can someone please explain why sq() works and add() does not? How can add() be fixed? What is use of non type templates?

template <typename T>
inline T sq (const T& x) 
{
    return x*x;
}

template <int*>
inline int add(int* x)
{
   return (*x)*2;
}



int main() {
int x = 2;
cout<<"Square(2): "<<sq(2)<<" Add(2): "<<add(&x)<<endl;
return 0;
}

Even after modifying the above example as below, it would still Not work

template <typename T>
inline T sq (const T& x) 
{
    return x*x;
}

template <int>
inline int add(int x)
{
   return x+x;
}



int main() {
    cout<<"Square(2): "<<sq(2)<<" Add(2): "<<add(2)<<endl;
    return 0;
}
  • 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-27T22:22:55+00:00Added an answer on May 27, 2026 at 10:22 pm

    I’m not quite sure what you’re asking, but you could use a non-type template parameter to, for example, define a function template that adds any compile-time constant to its argument:

    template <int N>
    inline int add(int x)
    {
        return x + N;
    }
    
    int main()
    {
        std::cout << add<3>(2) << std::endl; // prints 5
        std::cout << add<4>(2) << std::endl; // prints 6
    }
    

    To answer your specific question about why sq(2) compiles but add(&x) doesn’t: type parameters of function templates can be deduced from the function arguments. So sq(2) is equivalent to sq<int>(2). Non-type parameters can’t be deduced, so you have to provide one. In the case of a pointer parameter, the argument must be a pointer to a variable with external linkage, so the following should compile:

    int global;
    
    int main() {
        int local = 2;
        std::cout << add<&global>(&local) << std::endl;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to learn how to use jQuery templates using this old ScottGu's blog
We're trying to learn to use T4 Templates. I have a desire to use
I am trying to learn & create Visual Studio templates and as per this
Trying to learn MVP pattern with C#... Does anyone know of any particularly good
Can someone enlighthen me with regards to Linq please? I have a dataset that
I'm trying to learn templates and I've run into this confounding error. I'm declaring
Im trying to learn class-based views, for a detail or list view is not
I am trying to learn xslt but have no good tutorials where i can
im trying to learn about regex, and how to use it braking up som
I'm trying to learn templates in C++ and one of the things I was

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.