C++ Logo

std-discussion

Advanced search

Re: std::valarray<T>: deduction guide.

From: Andrew Schepler <aschepler_at_[hidden]>
Date: Tue, 8 Oct 2019 23:05:57 -0400
(You mean -> valarray<T>; not -> valarray;)

Yes, I think the described deduction guide is correct as written in the
Standard and will work correctly. There is no requirement that a deduction
guide has exactly the same parameter types as a constructor. Class template
argument deduction deduction involves an overload resolution process, but
that's entirely independent of the overload resolution process which then
chooses a constructor of the deduced class type. After the specific class
type is determined, it just needs to be valid to construct that class from
the initializer in some way, following the usual rules. When the deduced
class type is defined by a partial or explicit instantiation rather than
the primary class template, the constructors used for initialization can be
entirely different from the primary class constructors which were used in
deducing the class template arguments.

In this case, if a placeholder class type is deduced by selecting the
deduction guide
    template<class T, size_t cnt> valarray(const T(&)[cnt], size_t) ->
valarray<T>;
then the constructor used will be
    valarray(const T*, size_t);
via the array-to-pointer conversion.

I think if the deduction guide were not specified, then code like
    const double a[] = {1.0, 1.2, 2.9};
    std::valarray va(a, sizeof(a)/sizeof(*a));
would use
    valarray(const T&, size_t)
to deduce T as const double[3] and get decltype(va) as std::valarray<const
double[3]>, which won't work as likely intended, if at all.

But as it is, that code can use the deduction guide to deduce T as double,
so decltype(va) is std::valarray<double>, and the initializer (a,
sizeof(a)/sizeof(*a)) can initialize the va object using the
valarray<double>::valarray(const double*, size_t) constructor.



On Tue, Oct 8, 2019 at 11:41 AM Vladimir Grigoriev via Std-Discussion <
std-discussion_at_[hidden]> wrote:

> For the class std::valarray<T> there is the following deduction guide
>
> template<class T, size_t cnt> valarray(const T(&)[cnt], size_t) ->
> valarray;
>
> However such a constructor is not listed among constructors of the class
> std::valaary<T>.
>
> Is it correct?
>
> With nest regards,
> Vlad from Moscow
>
> You can meet me at http://cpp.forum24.ru/ or www.stackoverflow.com or
> http://ru.stackoverflow.com
> --
> Std-Discussion mailing list
> Std-Discussion_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-discussion
>

Received on 2019-10-08 22:08:22