Title
Why doesn't std::convertible_to have semantic requirement when To is reference-to-function type?
Status
new
Section
[concept.convertible]
Submitter
S. B. Tam

Created on 2020-06-30.00:00:00 last changed 54 months ago

Messages

Date: 2020-07-15.00:00:00

[ 2020-07-12; Reflector prioritization ]

Set priority to 3 after reflector discussions.

Date: 2020-06-30.00:00:00

[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
History
Date User Action Args
2020-07-12 16:33:28adminsetmessages: + msg11362
2020-06-30 00:00:00admincreate