tr1::array initialization

Douglas Gregor dgregor@osl.iu.edu
Wed May 7 23:34:00 GMT 2008


On May 6, 2008, at 9:19 AM, Benjamin Kosnik wrote:

>
>> I've searched this list as well as google and the c++ committee
>> information I've found on gcc.gnu.org and haven't seen any discussion
>> about this nice-to-have initialization of tr1::array:
>>
>> tr1::array<int> a = { 1, 4, 6 }; // instead of tr1::array<int,N> a =
>> {...}
>>
>> I was disappointed when I first learned of the tr1::array because I
>> was sure that an initializer would be allowed similar to
>>
>> int a[] = {1, 4, 6};
>>
>> Why can't we let the compiler figure out the N?
>>
>> Such an initializer would help in auto-generated code when N is not
>> known in advance.
>
> I think you made a pretty strong case here. You'd probably be best off
> to write up a specific proposal with the wording you desire for a
> variadic N, and submit it to the LWG issues list.

This is a language issue, not a library issue. Tom wants the N  
deduced from the initializer, but doing so would require tr1::array  
to be recognized by the compiler as magic; at least, I can't think of  
any extension that makes this not magic. Here's an alternative  
formulation that does work:

Add a make_array function that builds an array from the arguments it  
receives, e.g.,

	template<typename T, typename... Args>
	inline array<T, sizeof...(Args)> make_array(Args&&... args) {
		return { std::forward<Args>(args)... } ; // assumes we have  
generalized initializer lists in C++0x!
	}

Then, the way to build an array from a list of unknown size is:

	auto a = make_array<T>(1, 4, 6);

	- Doug



More information about the Libstdc++ mailing list