Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call void foo(int (&)[]) {} in C++?

void f1(int (&)[8])
{}

void f2(int (&)[])
{}

int main()
{
    int a[8];

    f1(a); // OK

    f2(/* What should I put here? */); // ???

    return 0;
}

How do I call f2?

PS: void f2(int (&)[]) {} is legal under VC++ 2012.

consider the following:

template<class T>
struct A
{};

template<class T>
struct A<T[]>
{};

template<class T, size_t size>
struct A<T[size]>
{};
like image 813
xmllmx Avatar asked Dec 26 '22 16:12

xmllmx


1 Answers

C++ has an explicit rule that disallows references or pointers to arrays without bounds as parameters (but those are otherwise valid types). The following would be a valid argument to such a parameter

extern int arg[];

Note that you cannot use an array with a size. C++ does not have the type compatibility concept. C has, and makes an array type without size compatible to the corresponding array type with a size. In C++, the typesystem is stricter, types have linkage and prototypeless function types do not exist, so type compatibility is not a real need, so C++ dropped it.

like image 195
Johannes Schaub - litb Avatar answered Jan 09 '23 04:01

Johannes Schaub - litb