Why doesn’t my example satisfy this "requires" expression?

I am attempting to use concepts with a variadic template. My template class is below. The concept should say: "Each type T should have a member function Func that accepts an input of type FuncArgType."

#pragma once

#include <tuple>
#include <concepts>
#include <iostream>

template<typename FuncArgType, typename... Ts>
requires requires
(FuncArgType func_arg, std::tuple<Ts...> args) {
    std::apply([&func_arg](auto&... ts) {(ts.Func(func_arg), ...); }, args);
}
class MyClass
{
public:
    MyClass() {}
};

This appears to me to be correct, although there might be an error with my use of std::apply. I then try to create an object using MyClass. I define two classes A and B to be my Ts. FuncArgType is int.

class A {
public:
    A() {}
    void Func(int i) {
        std::cout << "Called Func of A";
    }

};


class B {
public:
    B() {}
    void Func(int i) {
        std::cout << "Called Func of B";
    }

};

I now try to create an instance of MyClass.

#include "MyClass.h"

int main() {
    MyClass<int, A, B> my_class{}; // constraints not satisfied

    return 0;
}

The error message is

Source.cpp(4,19): error C7602: 'MyClass': the associated constraints are not satisfied

(There are several other error messages, but they all essentially say that my_class is invalid.)

Why don’t A and B satisfy my constraint? Does my constraint say what I want it to say? How do I make my constraint say the above bolded statement?

Source: Windows Questions C++

LEAVE A COMMENT