This page is a snapshot from the LWG issues list, see the Library Active Issues List for more information and the meaning of New status.

3459. Why doesn't std::convertible_to have semantic requirement when To is reference-to-function type?

Section: 18.4.4 [concept.convertible] Status: New Submitter: S. B. Tam Opened: 2020-06-30 Last modified: 2020-07-12

Priority: 3

View other active issues in [concept.convertible].

View all other issues in [concept.convertible].

View all issues with New status.

Discussion:

18.4.4 [concept.convertible] p2:

Types From and To model convertible_to<From, To> only if:

  1. (2.1) — To is not an object or reference-to-object type, or static_cast<To>(f()) is equal to test(f).

  2. […]

This requires the implicit and explicit conversions to produce equal results.

However, it seems that when To is a reference-to-function type, this restriction does not apply. This makes it possible to create a class that models convertible_to, but produces different results depending on the kind of conversion:

#include <concepts>

int foo() { return 0; }
int bar() { return 42; }

using FT = int();
struct A 
{
  operator FT&() const { return foo; }
  explicit operator FT&() { return bar; }
};

static_assert(std::convertible_to<A, FT&>);

A a;
FT& x = a;                    // x == foo
auto y = static_cast<FT&>(a); // y == bar

[2020-07-12; Reflector prioritization]

Set priority to 3 after reflector discussions.

Proposed resolution: