Skip to content

Compile error on implicit construction of T from another type #27

@yoursunny

Description

@yoursunny

Snippet to reproduce:

// g++ -std=c++14 -o x x.cpp

#include "optional.hpp"

class A
{
};

// B is implicitly constructible from A.
class B
{
public:
  B(A a)
  {
  }
};

// f accepts optional<B>.
void f(nonstd::optional<B> b)
{
}

// check conditions on https://en.cppreference.com/w/cpp/utility/optional/optional item 8
static_assert(std::is_constructible<B, A&&>::value, "");
static_assert(!std::is_same<std::decay_t<A&&>, nonstd::in_place_t>::value, "");
static_assert(!std::is_same<std::decay_t<A&&>, nonstd::optional<B>>::value, "");

int main()
{
  A a;

  f(a); // Call f with A, compile error.
}

This snippet fails to compile on gcc 5.4 in C++14 mode:

$ g++ -std=c++14 -o x x.cpp
x.cpp: In function ‘int main()’:
x.cpp:31:6: error: could not convert ‘a’ from ‘A’ to ‘nonstd::optional_lite::optional<B>’
   f(a);
      ^

It works on gcc 8.2 in C++17 mode: https://godbolt.org/z/2XyZum

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions