0
#include <stdio.h>
#include <iostream>

using namespace std;

template <typename T, int N>
int ReturnArraySize(T (&arg1)[N]) {
    return N;
}

constexpr int ReturnTheSum(int arg1, int arg2) {
    return arg1 + arg2;
}

int main(int argc, char **argv)
{
    int arr1[20];
    int arr2[ReturnArraySize(arr1)];
    int arr3[ReturnTheSum(ReturnArraySize(arr1), ReturnArraySize(arr2))];

    return 0;
}

When I compile the code, I get the following error:

/root/Documents/C++11_Fundamentals/ConstExprRelatedFunc/main.cpp:19:67: error: no matching function for call to 'ReturnArraySize(int [(<anonymous> + 1)])'

1
  • I want to know the reason why this error is coming Commented Mar 31, 2016 at 15:35

1 Answer 1

2

Because ReturnArraySize is not marked as a constexpr function, arr2 becomes a VLA (variable-length array, a GCC extension, not part of the C++ standard), which cannot be queried for its size at compile time (i.e., deduced by a function template).

You can fix this by making ReturnArraySize a constexpr:

template <typename T, int N>
constexpr int ReturnArraySize(T (&arg1)[N]) {
//~~~~~~^
    return N;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks my issue got fixed

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.