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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T14:21:05+00:00 2026-05-12T14:21:05+00:00

a template parameter can be used in another template parameter that follows it this

  • 0

a template parameter can be used in another template parameter that follows it this way :

template<typename T, T N>
struct s
{
};

But is it possible to reference “T” if it is declared after “N” ?

This does not work :

template<T N, typename T>
struct s
{
};

Can we help the compiler by pre-declaring “T” or doing anything else ?

Thanks by advance.

EDIT : as the first two replies were asking “why are you willing to do that ?” I’ll explain the goal :

I would like to make the compiler infer the type “T” in order to make the use of templated classes easier.

For example :

template<typename T, T A, T B>
struct sum
{
    static T const value = A + B;
};

This template can be used this way :

sum<int, 1, 2>::value

But it would be better if it could be used this way :

sum<1, 2>::value

Technically it’s should be possible because the compiler knows the types of “1” and “2” : “int”, and in fact it uses these informations to find the best overload for a function.
So by declaring the template this way :

template<T A, T B, typename T>
struct sum
{
    static T const value = A + B;
};

the compiler could use its capability to infer the last parameter from the informations provided by the first and the second one, and then find the best template to instantiate.

  • 1 1 Answer
  • 1 View
  • 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-12T14:21:06+00:00Added an answer on May 12, 2026 at 2:21 pm

    Like others say – No this isn’t possible, the compiler can’t infer the type of T from the non-type template arguments (in the case of functions, it infers types from the function arguments):

    14.8.2.4/12:

    A template type argument cannot be deduced from the type of a non-type template-argument.

    In any case, no deduction will be made for the arguments of a class template anyway. An example for a function template might be

    template<int> struct having_int { };
    template<typename T, T i> void f(having_int<i>);
    int main() { having_int<0> h; f(h); }
    

    In this case, T won’t be deduced as int – you have to explicitly specify it.

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

Sidebar

Ask A Question

Stats

  • Questions 189k
  • Answers 189k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer try adding the following: a{ background: transparent !important; } a… May 12, 2026 at 5:44 pm
  • Editorial Team
    Editorial Team added an answer I doubt it. Allowing web page scripts to pass input… May 12, 2026 at 5:44 pm
  • Editorial Team
    Editorial Team added an answer Most likely what you're looking for is _LP64, which is… May 12, 2026 at 5:44 pm

Related Questions

I'm looking at some codes which makes heavy uses of templates. It compiles fine
I wrote a small PHP application that I'd like to distribute. I'm looking for
I'm building a heavily CRUD based ASP.NET website and I've got to the phase
When researching an answer to a question (based on this answer ) I tried
I am building a multi-user web application. Each user can have their own site

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.