Function template problem

xiaoxiao2021-03-06  100

I have a problem in the 9CBS forum today.

#include

Using namespace std;

Template

Void min (const type array [size)

{COUT << Array [2];

Cout << Endl;

COUT << "The size of it: << size;

}

void main ()

{Int a [] = {1, 3, 4, 5, 6, 7, 9};

MIN (a);

}

This program has errors in VC6, because Size in MIN (a) cannot be thrown out.

This template function is min (const type array [size]), this parameter is written, but it is: const type * array. When MIN (a) is instantiated, two conversions are used in the template prunity, one is the left value conversion, one is defining modifier conversion. From int a [] à int * a à const * a. Obviously, the parameter transmission is only a pointer, which cannot know the size of the array.

There are two ways to solve:

1. Explicit template

MIN

(a);

2. Rewriting the template function declaration

1) Void min (const type * array, int size)

Institecation: MIN (a, 7);

2) Void min (& Array) [Size] // This cannot be written here, because this is not the same type with the first type of INT (a) [], which cannot be matched.

Installation: min (a)

But I found 2) Although it meets the standard (can be compiled with GCC), but cannot be compiled by VC6, there is an error:

Error C2265: '

': Reference to a Zero-Size Array Is Illegal

This is a bit of solution. More and more discovered that VC6 really doesn't comply with C standards and is a bit disappointed. But fortunately, you can compile in VC7 :)

2004-8-21

转载请注明原文地址:https://www.9cbs.com/read-124802.html

New Post(0)