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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T09:16:43+00:00 2026-06-05T09:16:43+00:00

#include <iostream> template <int M, int N> void print1(int src[M][N]) { } void print2(int

  • 0
#include <iostream>

template <int M, int N>
void print1(int src[M][N]) { }

void print2(int src[4][4]) { }

int main() {

    int src[][4] = {
        { 1,  2,  3,  4},
        { 5,  6,  7,  8},
        { 9, 10, 11, 12},
        {13, 14, 15, 16},
    };

    print1(src);
    // gives error
    // error: no matching function for call to 'print1(int [4][4])'

    print2(src);
    // works!
}

In code above, print2() works as expected, but print1() gives me the error

error: no matching function for call to ‘print(int [4][4])’

I don’t understand, they look exactly the same, I just replaced the hard coded values to use templates so that it can accept arrays of any size.

Why does it not work? What am I doing wrong?

  • 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-05T09:16:44+00:00Added an answer on June 5, 2026 at 9:16 am

    In the declaration

    void print2(int src[4][4])
    

    The first 4 is meaningless. This function is the same as if you had declared it as

    void print2(int src[][4])
    

    or as

    void print2(int (*src)[4])
    

    This is because arrays are never passed by value in C and C++. Rather, when an array is passed to a function, it is implicitly converted to a pointer to its initial element. Likewise, when a function parameter has the type “array of T“, it is transformed automatically to be of type “pointer to T.” In effect, there are no array type parameters in C and C++.

    So, let’s consider your function template:

    template <int M, int N>
    void print1(int src[M][N])
    

    Similar to print2, this function template is equivalent to:

    template <int M, int N>
    void print1(int src[][N])
    

    In order to call this function without explicitly stating the template arguments in the call, the compiler must be able to deduce what M and N are from the type of the argument. M doesn’t really appear anywhere in the argument list, so there’s no way for it to be deduced from the argument. You could call this function by explicitly providing the template arguments when you make the call:

    print1<4, 4>(src)
    

    As we saw above, however, the compiler can deduce N itself; it’s just M that it can’t deduce. Therefore, you can also make the call by only providing an argument for M and letting the compiler deduce N:

    print1<4>(src)
    

    Alternatively, you could declare the function template as taking a reference to an array:

    template <int M, int N>
    void print1(int (&src)[M][N])
    

    This suppresses the array-to-pointer conversion. Why? Remember that in the previous examples, the parameter was “a pointer to a single-dimensional array of int.” However, in this function template, the parameter is “a reference to a two-dimensional array of int.” Both extents (dimensions) are part of the type, and therefore both can be deduced by the compiler.


    However, in most cases it is best to avoid multidimensional arrays and references to arrays because they are cumbersome. Neither works well with dynamic allocation, and it’s often a lot of trouble to keep the array-to-pointer conversion from occurring.

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

Sidebar

Related Questions

I have template function compare defined as below. #include<iostream> using namespace std; template<typename T>
In the following code: #include <iostream> template <typename T, size_t N> void cal_size(T (&a)[N])
EDIT: I realized that this code compiles and works: #include <iostream> template<class Something> class
Let's start with this demo #include <iostream> using namespace std; template <class T> void
Please consider this code: #include <iostream> template<typename T> void f(T x) { std::cout <<
The following code gives a compiler error (gcc-4.7 run with -std=c++11 ): #include <iostream>
I wrote the following program #include <iostream> template<typename C, typename Res, typename... Args> class
#include <cstdlib> #include<iostream> using namespace std; template <typename T> class stack { public: T
I want to do the following :- #include <iostream> template <typename I> class A
I'm trying to understand why this snippet fails: #include <iostream> using namespace std; 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.