| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"John Goche" |
| Date: |
03 Sep 2006 01:08:52 PM |
| Object: |
numbers in templates |
Hello,
I was wondering what the pros and cons are for using
numbers in templates rather than in constructors as in:
MyBuffer<10> foo;
rather than
MyBuffer foo(10);
Thanks,
JG
.
|
|
| User: "Frederick Gotham" |
|
| Title: Re: numbers in templates |
03 Sep 2006 01:15:27 PM |
|
|
John Goche posted:
MyBuffer<10> foo;
rather than
MyBuffer foo(10);
That question cannot be answered without context.
The former is more restrictive, in that you need a separate type for each
integral value.
--
Frederick Gotham
.
|
|
|
|
| User: "Thomas J. Gritzan" |
|
| Title: Re: numbers in templates |
03 Sep 2006 01:36:38 PM |
|
|
John Goche schrieb:
Hello,
I was wondering what the pros and cons are for using
numbers in templates rather than in constructors as in:
MyBuffer<10> foo;
rather than
MyBuffer foo(10);
Think of MyBuffer as some kind of array/vector.
With MyBuffer<10> you would have an array of fixed size. The size has to be
known at compile-time and has to be a compile-time constant, as with plain
arrays. Each template instantiation forms a new _type_ (the size is part of
the type), and this types are not interchangeable (you cannot pass a
MyBuffer<10> where a MyBuffer<3> is expected). This way is usefull, for
example, for vectors in maths, where it doesn't make sense to add a
3D-vector to a 4D-vector.
The size as constructor parameter can be a dynamic value, determined at
run-time. As in std::vector, the size can be changed later and is not part
of the type.
--
Thomas
http://www.netmeister.org/news/learn2quote.html
.
|
|
|
| User: "John Goche" |
|
| Title: Re: numbers in templates |
04 Sep 2006 09:20:13 AM |
|
|
Thomas J. Gritzan wrote:
John Goche schrieb:
Hello,
I was wondering what the pros and cons are for using
numbers in templates rather than in constructors as in:
MyBuffer<10> foo;
rather than
MyBuffer foo(10);
Think of MyBuffer as some kind of array/vector.
With MyBuffer<10> you would have an array of fixed size. The size has to be
known at compile-time and has to be a compile-time constant, as with plain
arrays. Each template instantiation forms a new _type_ (the size is part of
the type), and this types are not interchangeable (you cannot pass a
MyBuffer<10> where a MyBuffer<3> is expected). This way is usefull, for
example, for vectors in maths, where it doesn't make sense to add a
3D-vector to a 4D-vector.
There seems to be a lot of overhead to this approach since a type has
to be created for each size. What are the performance benefits
associated with knowing the size at compile time, and how do these
outweigh the drawback of codesize bulk due to having to create
multiple types?
Thanks,
JG
.
|
|
|
| User: "Thomas J. Gritzan" |
|
| Title: Re: numbers in templates |
04 Sep 2006 04:36:17 PM |
|
|
John Goche schrieb:
There seems to be a lot of overhead to this approach since a type has
to be created for each size. What are the performance benefits
associated with knowing the size at compile time, and how do these
outweigh the drawback of codesize bulk due to having to create
multiple types?
This can't be answered in general so easily.
It depends, and should be measured when you have a concrete class.
Types by itself are no entities saved in you binary, but the functions
generated increase the size. For inlined functions, that should make no
difference.
It all depends on your compiler/linker, the optimization level, and of
course, your class design.
--
Thomas
http://www.netmeister.org/news/learn2quote.html
.
|
|
|
|
|
|

|
Related Articles |
|
|