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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:26:48+00:00 2026-06-12T06:26:48+00:00

Im trying to deduce a non-type template argument. #include <iostream> template <unsigned int S>

  • 0

Im trying to deduce a non-type template argument.

#include <iostream>

template <unsigned int S>
void getsize(unsigned int s) { std::cout << s << std::endl; }

int main()
{
  getsize(4U); 
// Id like to do this without explicitly stating getsize<4U>(4);
// or even getsize<4U>(); 
// Is this possible?
}

But I am getting error:

deduce_szie_t.cpp: In function 'int main()':
deduce_szie_t.cpp:9:15: error: no matching function for call to 'getsize(unsigned int)'
deduce_szie_t.cpp:9:15: note: candidate is:
deduce_szie_t.cpp:4:6: note: template<unsigned int <anonymous> > void getsize(unsigned int)
deduce_szie_t.cpp:4:6: note:   template argument deduction/substitution failed:
deduce_szie_t.cpp:9:15: note:   couldn't deduce template parameter '<anonymous>'

Is it possible to deduce the unsigned int, without having to explicitly state the template parameter?

Id like to have it clean like: getsize(4U)
I want to avoid writing: getsize<4U>()

Thanks alot for any help

  • 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-12T06:26:49+00:00Added an answer on June 12, 2026 at 6:26 am

    It is possible to deduce a non-type template argument from function arguments, but not in the way you want. It can only be deduced from the type of the function argument, not from the value.

    For example:

    template <unsigned int S>
    void getsize(int (*s)[S]) {
        std::cout << "pointer value: " << (void*)s << std::endl;
        std::cout << "deduced size: " << S << std::endl;
    }
    
    getsize(static_cast<int (*)[4]>(0));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am basically trying to do the same as std::enable_if : parameter vs template
I've been trying to implement a function that needs partial template specializations and fallen
I'm working on Objective C and I'm trying to deduce the proper index of
im trying to take std::string value and use it as delimiter in std::getline() function
I am trying ot add a function template that will print if it contains
I am trying to define an algebric type: data MyType t = MyType t
I've been trying to call the overloaded table::scan_index(std::string, ...) member function without success. For
I'm learning Haskell and stuck trying to understand the type system. I'm trying to
I am trying to compose a function of type (Floating a) => a ->
In trying to compile this program: namespace MyNamespace { template<typename T> class Test {

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.