Skip to content

<future>: promise default constructs the value type #2488

@eiband

Description

@eiband

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.

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