C++20: Concepts and CRTP
23:20 04 Nov 2024

I have trying to use concepts and CRTP pattern in my project but I see compilation errors using both gcc-12 and MSVC 2022. Here is the simplified code:

namespace Test
{
    namespace concepts {
        template
        concept SupportsFn = requires(T t, int k) {
            {
                t.fn(k)
            };
        };
    }

    template
    class CD {
        static_assert(concepts::SupportsFn);
    public:
        void call(int k) {
            this->fn(k);
        }
    };

    template
    class BD : public CD {
        static_assert(concepts::SupportsFn);
    public:
        void call2(int k) {
            this->fn(k + 2);
        }
    };
}

class ED : public Test::BD {
public:
    void fn(int k) {
        std::cout << "FN " << k << " \n";
    }
};

int main() {


    ED ed{};
    ed.call(10);

    return 0;
}

Both GCC and MSVC have compilation error that template constraints are not satisfied because of incomplete type ED. Here is an example of the MSVC error:

>  C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Tools\MSVC\14.38.33130\include\expected(11): warning STL4038: The contents of  are available only with C++23 or later.
1>  C:\temp\memfnexpt\memfnexpt\memfnexpt.cpp(377,25): error C7602: 'Test::BD': the associated constraints are not satisfied
1>      C:\temp\memfnexpt\memfnexpt\memfnexpt.cpp(368,2):
1>      see declaration of 'Test::BD'
1>      C:\temp\memfnexpt\memfnexpt\memfnexpt.cpp(367,11):
1>      the concept 'Test::concepts::SupportsFn' evaluated to false
1>          C:\temp\memfnexpt\memfnexpt\memfnexpt.cpp(353,5):
1>          use of undefined type 'ED'
1>          C:\temp\memfnexpt\memfnexpt\memfnexpt.cpp(377,7):
1>          see declaration of 'ED'
1>          C:\temp\memfnexpt\memfnexpt\memfnexpt.cpp(353,7):

How do I use concepts with CRTP and get around this error?

c++ c++20 crtp