[Bug c++/15674] template argument binding differs between member and static fumctions

igodard at pacbell dot net gcc-bugzilla@gcc.gnu.org
Thu May 27 19:49:00 GMT 2004


------- Additional Comments From igodard at pacbell dot net  2004-05-27 10:42 -------
Nathan asks for field experience, so here's what I was doing when I found the problem. 

I have a template type Row<T> that acts as a simple pointer-and-count descriptor for one dimensional slices; a poor man's valarray. It is used for non-numeric data manipulation and to provide an encapsulated package for the innumerable places where otherwise a function has to take separate pointer and count args, as you find throughout the standard libraries. I don't use valarray because that has a whole lot of numeric baggage and lacks a number of additional Row-manipulating functions that my code uses (like modulo inner-hull). I suppose I could have derived from valarray, but this is actually a port from C and I wanted to make Rows be STL-compatible containers for use with standard algorithms.

For convenience there are a number of utility constructors: () [empty]; (T*) [Row is length one and describes the poined-to element); (Row) [copy constructor]; and (T(&)[]) [Row describes entire array]. I want to be able to say:
int arr[] = {1,2,3,4,5};
Row<int> r(arr);
and wind up with r with a length of 5. If the reference to arr is turned into a pointer then it matches the T* overload and produces an array of length 1, a bug. Then to describe the whole array I'd have to write:
Row<int> r(arr, 5);
passing in the length explicitly. This would be a source of maintenance bugs every time the arr table changed in size, which in my code is frequent during development. Because this is a constructor I can't use different names to distingish the pointer from the array case, except by adding an intermediate function:
template<typename T, size_t n>
Row<T> Foo(T(&)[n]);
Row<int> r(Foo(arr));
which is clumsy.

I hope that the standard will permit an overload to match an array as distinct from a pointer, in all contexts. Sometimes you really want to know. I hope this explanation has been helpful.

Ivan

-- 
           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|[DR214] template argument   |template argument binding
                   |binding differs between     |differs between member and
                   |member and static fumctions |static fumctions


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=15674



More information about the Gcc-bugs mailing list