On Monday, January 11, 2016 at 6:33:52 AM UTC+1, Alf P. Steinbach wrote:
> On 1/11/2016 1:05 AM, Stefan Ram wrote:
> > »Ignoring defaulted optional allocator parameters for
> > simplicity, the two constructors are: vector( size_t n,
> > const int& value ); // A: n copies of value«
> >
> >
herbsutter.com/2013/05/09/gotw-1-solution/
> >
> > Why is there a reference in »const int& value« instead of
> > just »const int value«?
>
> Because `std::vector` is a class template, where this argument type is
> specified in terms of a template parameter.
>
> It's not practical to define a type-dependent choice of a formal
> argument type in a template, because that would foil ordinary template
> argument type deduction.
Well, it is possible to do something type-dependent and template
argument deduction is of no concern in this case. But I would claim
that something like this...
template<class T> // intentionally ignoring allocators
class vector {
...
vector(size_type n,
typename boost::call_traits<T>::param_type value = T());
...
};
...is not worth the effort at all. But this would pick pass-by-value
for small trivial types and pass-by-const-ref otherwise. So,
technically, it is possible.
Cheers!
SG