microsoft / microsoft/STL

`<future>`: `promise` default constructs the value type

Open
#2,488 5 comments 0 reactions 0 assignees View on GitHub

A pull request for this has already been merged.

  • #2568 by @MitalAshok — merged
bug vNext
Dominant language
C++
Stars
11.1k
Forks
1.7k
Avg merge
4d 15h
Merged PRs (30d)
22

Description

Describe the bug
The value type T of a promise<T> is default constructed when the promise is constructed, which is surprising. I am not sure what the requirements of the standard are. The current implementation requires T to be default constructible and assignable. It is possible to implement promise with none of these two requirements.

Command-line test case

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

struct test
{
  test()
    : result_{-1}
  {
    std::cout << "Default constructor" << std::endl;
  }

  explicit test(int i)
    : result_{i}
  {
  }

  int result() const
  {
    return result_;
  }

private:
  int result_;
};

int main()
{
  std::promise<test> prm;
  std::future<test> fut = prm.get_future();

  prm.set_value(test{0});

  return fut.get().result();
}

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

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

/out:repro.exe
repro.obj

C:\Temp>.\repro.exe
Default constructor

Expected behavior
The line "Default constructor" should not be printed. Moreover, replacing the default constructor test() by

test() = delete;
test(const test&) = default;
test& operator=(const test&) = delete;

should still compile.

STL version
Microsoft Visual Studio Professional 2019
Version 16.11.9

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.

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 with the command-line reproduction and the expected behavior described in the issue. Review the vNext and binary-compatibility constraint, along with the history of merged PR #2568. Done means promise no longer default-constructs T and supports the shown non-default-constructible, non-assignable type.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
tooling
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.