Skip to content

<valarray>: slice_array/gslice_array incorrectly bind to function arguments of type slice/gslice #1630

@statementreply

Description

@statementreply

Describe the bug

MSVC STL's slice_array and gslice_array derive from slice and gslice, respectively.

STL/stl/inc/valarray

Lines 1382 to 1384 in 47cd703

// CLASS TEMPLATE slice_array
template <class _Ty>
class slice_array : public slice { // define a slice of a valarray

STL/stl/inc/valarray

Lines 1560 to 1562 in 47cd703

// CLASS TEMPLATE gslice_array
template <class _Ty>
class gslice_array : public gslice { // define a generalized slice of a valarray

When a function overload set with an overload for both valarray<bool> and slice is called with a slice_array<bool> argument, the valarray<bool> overload should be selected (slice_array<bool> is implicitly convertible to valarray<bool>) according to the standard. However, with MSVC STL, the slice overload is incorrectly selected because it is the base class of slice_array<bool>, and derived-to-base conversion is better than user-defined conversion in overload resolution.

Command-line test case

D:\Temp>type slice.cpp
#include <cassert>
#include <valarray>
using namespace std;

int main() {
    valarray<int> a(10);
    valarray<bool> b(false, 20);
    valarray<int> c = a[b[slice(0, 10, 2)]];
    assert(c.size() == 0);
    return 0;
}
D:\Temp>cl /EHsc /W4 /WX slice.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 19.28.29812 for x64
Copyright (C) Microsoft Corporation.  All rights reserved.

slice.cpp
Microsoft (R) Incremental Linker Version 14.28.29812.0
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:slice.exe
slice.obj

D:\Temp>.\slice.exe
Assertion failed: c.size() == 0, file slice.cpp, line 9

STL version

c7b1405

Additional context

This bug is discovered when reviewing #1627, where slice_array<int> == slice_array<int> incorrectly calls the newly added operator==(const slice&, const slice&).

vNext note: Resolving this issue will require breaking binary compatibility. We won't be able to accept pull requests for this issue until the vNext branch is available. See #169 for more information.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingvNextBreaks binary compatibility

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions