0

I guess the title is quite confusing, I'll explain my case with some code.

template<uint16_t Len>
void add(const int8_t (&i_array)[Len])
{
  // Do something
}

class Test
{
public:
int8_t* GetName()
{
  return name;
} 

private:
 int8_t name[10] = "myname";
}

int main()
{
 Test mytest;

 add(mytest.GetName()); // Compilation error
}

This code does not compile. The following error is generated : "Error#304 : no instance of function template add matches the argument list"

It seems that the compilator is not able to determine that GetName() return an array of size 10. Is that right ?

How could I call "add" with a pointer on an array ?

Thanks, Nicolas

0

1 Answer 1

2

Test::GetName returns a pointer, not an array. You cannot bind its result to a function that expects an array reference. However, you could change the signature of GetName to make it return the array (by reference, of course):

int8_t (&GetName())[10] { return name; }

Alternatively you could use a cast, but that would defeat the purpose of the type system.

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

2 Comments

Nice, I'll try it now.
BTW, I suggest to use a typedef to have a simpler syntax.

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.