Removing C ++ Type Concepts

In the N4622 range specification, the concept is Samedefined for two types Tand U, but is sometimes used internally in requiresonly one way:

{ t } -> Same<T>;

What is the rule that allows you to subtract for the second type U? (e.g. from specification N4630 )

The simplest such example:

template <class T, class U>
concept bool C = (sizeof(T) == sizeof(U)) && (sizeof(U) != 1);

template <class T>
concept bool D = requires(T t){
  // How is U deduced here? 
  {t} -> C<T>; 
};

template <class T>
  requires D<T>
void fn() {}

int main() {
  // Fails with: unable to deduce placeholder type 'C<char>' from 't'
  // That expected.
  //fn<char>();

  // But how does this work?
  fn<int>();
}

Tested sample using g ++ 8.0.0 and -fconcepts.

+6
source share
1 answer

What is a placeholder?

, , : - , . - , . ( - [ ], stand-ins -. , -.)

, , auto decltype(auto):

// the specifiers are all placeholders for `int`
auto i = 42;
auto& lref = i;
auto&& rref = 0;
decltype(auto) = 2 * i;

:

// two placeholders for `int`
std::pair<auto, auto> p = std::make_pair(1, 4);

// one placeholder
std::pair<auto, int> q = p;

, : :

template<typename Int>
concept bool Integral = …;

// placeholder for `int` constrained by `Integral<int>`
Integral a = 0;

// same as above
Integral<> b = a;

template<typename Lhs, typename Rhs>
concept bool EqualityComparable = …;

// another placeholder for `int`
// this time constrained by `EqualityComparable<int, double>`
EqualityComparable<double> c = a;

binary EqualityComparable. , , , . , ( ) .

-, size(). , size() Incrementable ( - , Integral).

template<typename Incr>
concept bool Incrementable = requires(Incr incr) {
    ++incr;
};

template<typename Cont>
concept bool Sizeable = requires(Cont const cont) {
    // requirement #1
    { cont.size() } -> Incrementable
};

№ 1 . , , --. , :

template<Incrementable Incr>
void valid_for_incrementable(Incr incr);

template<typename Cont>
concept bool Sizeable = requires(Cont const cont) {
    cont.size();
    valid_for_incrementable(cont.size());
};

, , : .

, :

template<typename T>
concept bool Demo = requires(T t) {
    { t } -> C<T>;
};

, C<T, T> t. placeholder C<int>, C<T, int> ..

+1

Source: https://habr.com/ru/post/1016876/


All Articles