microsoft / microsoft/STL

<iostream>: Bitmasks should tolerate unconstrained operator overloads

Open
#292 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
C++
Stars
11.1k
Forks
1.7k
Avg merge
4d 15h
Merged PRs (30d)
22

Description

This should compile, but doesn't:

C:\Temp>type meow.cpp
template <typename T, typename U> void operator&(T&&, U&&) = delete;
template <typename T, typename U> void operator|(T&&, U&&) = delete;
template <typename T, typename U> void operator^(T&&, U&&) = delete;
template <typename T, typename U> void operator&=(T&&, U&&) = delete;
template <typename T, typename U> void operator|=(T&&, U&&) = delete;
template <typename T, typename U> void operator^=(T&&, U&&) = delete;
template <typename T> void operator~(T&&) = delete;

#include <iostream>

int main() {
    std::cout << 1729 << "\n";
}

C:\Temp>cl /EHsc /nologo /W4 meow.cpp
meow.cpp
S:\msvc\binaries\amd64chk\inc\xlocale(1059): error C2280: 'void operator &<const std::_Codecvt_mode&,std::_Codecvt_mode>(T,U &&)': attempting to reference a deleted function
        with
        [
            T=const std::_Codecvt_mode &,
            U=std::_Codecvt_mode
        ]
meow.cpp(1): note: see declaration of 'operator &'
S:\msvc\binaries\amd64chk\inc\xlocale(1125): error C2280: 'void operator &<const std::_Codecvt_mode&,std::_Codecvt_mode>(T,U &&)': attempting to reference a deleted function
        with
        [
            T=const std::_Codecvt_mode &,
            U=std::_Codecvt_mode
        ]
meow.cpp(1): note: see declaration of 'operator &'
[...19 more errors...]

This happens when the STL uses an enum as a bitmask:

https://github.com/microsoft/STL/blob/58bb49d63d92e7a0346a05af29816aeea6b4cf0f/stl/inc/xlocale#L894
https://github.com/microsoft/STL/blob/58bb49d63d92e7a0346a05af29816aeea6b4cf0f/stl/inc/xlocale#L1059

without defining dedicated bitmask operators:

https://github.com/microsoft/STL/blob/58bb49d63d92e7a0346a05af29816aeea6b4cf0f/stl/inc/type_traits#L2048-L2089

The test case has pathological overloads, but users can and have encountered this with reasonable code (typically, when they define templated operators to handle their own enums).

Curiously, this doesn't happen in /std:c++17 or /std:c++latest mode. I haven't analyzed why, but this is definitely a problem in /std:c++14 mode.

We should audit the entire STL for this problem.

Also tracked by Microsoft-internal VSO-115352 / AB#115352.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the C++14 failure with the issue's deleted operator overloads, then inspect the linked bitmask uses in stl/inc/xlocale and the bitmask definitions in stl/inc/type_traits. Audit the STL for similar unconstrained enum bitmask operations and verify that the reproduction compiles without errors while preserving the C++14 behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.