public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/113160] New: Const valarray (g)slice and indirect_array fail to match valarray in template functions
@ 2023-12-28  4:11 yihuajack at gmail dot com
  2023-12-28  5:00 ` [Bug libstdc++/113160] " pinskia at gcc dot gnu.org
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: yihuajack at gmail dot com @ 2023-12-28  4:11 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113160

            Bug ID: 113160
           Summary: Const valarray (g)slice and indirect_array fail to
                    match valarray in template functions
           Product: gcc
           Version: 13.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: yihuajack at gmail dot com
  Target Milestone: ---

Created attachment 56957
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=56957&action=edit
Output

GCC version: 13.2.0, etc.
System type: MinGW-w64 3.4.10.x86_64, etc.
Options: -std=c++23, etc.
Command line: g++ -std=c++23 -save-temps test2.cpp
Outputs are attached.
Reproducer:

#include <iostream>
#include <valarray>

template<typename T>
void foo(const std::valarray<T> &va) {
    std::cout << va[0] << '\n';
}

int main() {
    const std::valarray<bool> vmask = {1, 0, 0, 1};
    const std::valarray<std::size_t> vind = {1, 3};
    const std::valarray<double> va = {1, 2, 3, 4};
    foo(va[std::slice(1, 2, 1)]);
    foo(va[std::gslice(0, {1, 1}, {1, 1})]);
    foo(va[vmask]);
    foo(va[vind]);
}

The code can be successfully compiled by MSVC v19.38.33130 and v19.latest for
x86/x64 but failed to be compiled by GCC 13.2.0 or trunk.
If function foo() is not a template function, i.e.,

void foo(const std::valarray<double> &va) {
    std::cout << va[0] << '\n';
}

Then the code compiles.
If the valarray is not const, then it returns a std::valarray,
std::gslice_array, std::mask_array, or std::indirect_array, which makes no
dissent - the compiler refuses to match std::valarray with it. However, this is
not the case for a const valarray. According to the Working Draft, Standard for
Programming Language C++ (N4950):

Any function returning a valarray is permitted to return an object of another
type, provided all the
const member functions of valarray are also applicable to this type. This
return type shall not add
more than two levels of template nesting over the most deeply nested argument
type.
4 Implementations introducing such replacement types shall provide additional
functions and operators as
follows:
(4.1) — for every function taking a const valarray& other than begin and end
(28.6.10), identical functions
taking the replacement types shall be added;
(4.2) — for every function taking two const valarray& arguments, identical
functions taking every combi-
nation of const valarray& and replacement types shall be added.

For libstdc++, the replacement types (mask_array is an exception - the operator
function directly returns a valarray, so it compiles) are

_Expr<_SClos<_ValArray, _Tp>, _Tp>
_Expr<_GClos<_ValArray, _Tp>, _Tp>
_Expr<_IClos<_ValArray, _Tp>, _Tp>

which refuse to match const std::valarray<T> &, which might be a violation of
the C++ standard.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug libstdc++/113160] Const valarray (g)slice and indirect_array fail to match valarray in template functions
  2023-12-28  4:11 [Bug libstdc++/113160] New: Const valarray (g)slice and indirect_array fail to match valarray in template functions yihuajack at gmail dot com
@ 2023-12-28  5:00 ` pinskia at gcc dot gnu.org
  2023-12-28  6:19 ` yihuajack at gmail dot com
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-12-28  5:00 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113160

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
This is also fails with clang and libc++.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug libstdc++/113160] Const valarray (g)slice and indirect_array fail to match valarray in template functions
  2023-12-28  4:11 [Bug libstdc++/113160] New: Const valarray (g)slice and indirect_array fail to match valarray in template functions yihuajack at gmail dot com
  2023-12-28  5:00 ` [Bug libstdc++/113160] " pinskia at gcc dot gnu.org
@ 2023-12-28  6:19 ` yihuajack at gmail dot com
  2023-12-28 18:44 ` redi at gcc dot gnu.org
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: yihuajack at gmail dot com @ 2023-12-28  6:19 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113160

--- Comment #2 from Yihua Liu <yihuajack at gmail dot com> ---
It seems that llvm member considers it a bug, see llvm-project Issue #76450.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug libstdc++/113160] Const valarray (g)slice and indirect_array fail to match valarray in template functions
  2023-12-28  4:11 [Bug libstdc++/113160] New: Const valarray (g)slice and indirect_array fail to match valarray in template functions yihuajack at gmail dot com
  2023-12-28  5:00 ` [Bug libstdc++/113160] " pinskia at gcc dot gnu.org
  2023-12-28  6:19 ` yihuajack at gmail dot com
@ 2023-12-28 18:44 ` redi at gcc dot gnu.org
  2023-12-28 18:46 ` redi at gcc dot gnu.org
  2023-12-30 14:06 ` yihuajack at gmail dot com
  4 siblings, 0 replies; 6+ messages in thread
From: redi at gcc dot gnu.org @ 2023-12-28 18:44 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113160

--- Comment #3 from Jonathan Wakely <redi at gcc dot gnu.org> ---
> Any function returning a valarray is permitted to return an object
> of another type, provided all the const member functions of valarray
> are also applicable to this type. 

This means you can do v[0] on the type, it doesn't mean you can convert it to a
const valarray&

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug libstdc++/113160] Const valarray (g)slice and indirect_array fail to match valarray in template functions
  2023-12-28  4:11 [Bug libstdc++/113160] New: Const valarray (g)slice and indirect_array fail to match valarray in template functions yihuajack at gmail dot com
                   ` (2 preceding siblings ...)
  2023-12-28 18:44 ` redi at gcc dot gnu.org
@ 2023-12-28 18:46 ` redi at gcc dot gnu.org
  2023-12-30 14:06 ` yihuajack at gmail dot com
  4 siblings, 0 replies; 6+ messages in thread
From: redi at gcc dot gnu.org @ 2023-12-28 18:46 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113160

--- Comment #4 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Or more precisely, you can't deduce a const valarray<T> from it.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug libstdc++/113160] Const valarray (g)slice and indirect_array fail to match valarray in template functions
  2023-12-28  4:11 [Bug libstdc++/113160] New: Const valarray (g)slice and indirect_array fail to match valarray in template functions yihuajack at gmail dot com
                   ` (3 preceding siblings ...)
  2023-12-28 18:46 ` redi at gcc dot gnu.org
@ 2023-12-30 14:06 ` yihuajack at gmail dot com
  4 siblings, 0 replies; 6+ messages in thread
From: yihuajack at gmail dot com @ 2023-12-30 14:06 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113160

--- Comment #5 from Yihua Liu <yihuajack at gmail dot com> ---
I see. Previous cppreference notes were misleading. Is there any plan to
support returning a std::valarray or matching a std::valarray? For example, if
it returns a std::valarray, it can be directly put in a pipe syntax.

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2023-12-30 14:06 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-12-28  4:11 [Bug libstdc++/113160] New: Const valarray (g)slice and indirect_array fail to match valarray in template functions yihuajack at gmail dot com
2023-12-28  5:00 ` [Bug libstdc++/113160] " pinskia at gcc dot gnu.org
2023-12-28  6:19 ` yihuajack at gmail dot com
2023-12-28 18:44 ` redi at gcc dot gnu.org
2023-12-28 18:46 ` redi at gcc dot gnu.org
2023-12-30 14:06 ` yihuajack at gmail dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).