microsoft / microsoft/STL

`<array>`: `std::array<T,0>` calls constructors and destructors of `T`

Open
#5,583 7 comments 7 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Describe the bug

https://github.com/microsoft/STL/blob/7841cf88ff9af837fb980f2d2d4ac0f267e714c7/stl/inc/array#L761-L763

A std::array<T,0> is meant to be an array of zero elements, but it's actually an array of one element as long as T is default-constructible in this implementation. This is clearly non-compliant.

Command-line test case

C:\Temp>type repro.cpp
#include <array>
#include <iostream>

struct S { S() { std::cout << "test failure\n"; } };

int main() {
    std::array<S,0> s;
}

C:\Temp>cl /EHsc /W4 /WX /std:c++latest .\repro.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 19.36.32522 for x64
Copyright (C) Microsoft Corporation.  All rights reserved.

/std:c++latest is provided as a preview of language features from the latest C++
working draft, and we're eager to hear about bugs and suggestions for improvements.
However, note that these features are provided as-is without support, and subject
to changes or removal as the working draft evolves. See
https://go.microsoft.com/fwlink/?linkid=2045807 for details.

repro.cpp
Microsoft (R) Incremental Linker Version 14.36.32522.0
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:repro.exe
repro.obj

C:\Temp>.\repro.exe
test failure

Expected behavior

The program should print nothing.

STL version

https://github.com/microsoft/STL/commit/7841cf88ff9af837fb980f2d2d4ac0f267e714c7

Additional context

This is relevant to P3737R0: std::array is a wrapper for an array!

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

Start at stl/inc/array around lines 761-763 and reproduce the issue with the command-line example using a non-default-constructible or observable type. The fix is done when std::array<T, 0> performs no T construction or destruction and regression coverage confirms the program prints nothing.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.