2

I want to use call-by-reference with an array and found something on here which seemed to solve my problem. However I now changed it to this and I get the error message

"template argument deduction/substitution failed".

It works if I put

bool a[3];

but not with the variable m.

#include <assert.h>

template <typename T, int Size>
void dosth(T (&a)[Size])
{
    assert(Size > 2);

    a[2] = false;
}

int main()
{
    int m=3;
    bool a[m];
    dosth(a);
}
2
  • 1
    You should state your error message rather than "it doesn't work". Commented Nov 14, 2015 at 16:52
  • 1
    The bool a[m]; is known as a variable length array and not supported in C++. However, if you were to use const unsigned int for the capacity or #define for the capacity, it would be an array in local storage. Commented Nov 14, 2015 at 16:54

1 Answer 1

6

The bool a[m]; is known as a variable length array and not supported in C++.

However, if you were to use const unsigned int for the capacity or #define for the capacity, it would be an array in local storage.

If you want an array whose length is determined at run-time, consider using std::vector.

Sign up to request clarification or add additional context in comments.

2 Comments

This is a known issue with VLAs. Their current implementation (from what I have read elsewhere) is crufted-in such that they will break with things like this.
Thanks, I think this answers my question. This is just the first time I am using C++ and I'm used to working with array from Java. I'll consider using std::vector next time. Maybe I'll try it with const unsigned int.

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.