如果您想检查以下专业A
具体来说,这并不太难。
template <class C>
concept A_ = requires(C c) {
// IILE, that only binds to A<...> specialisations
// Including classes derived from them
[]<typename X>(A<X>&){}(c);
};
lambda 基本上只是重载接受的函数的简写A
专业化。从此类专业衍生的课程也计入其中。我们使用我们正在检查的类型的参数调用 lambda...并且约束是 true 或 false,具体取决于调用是否有效(参数被接受)。
然后,只需将其插入即可:
template <A_ T>
class B{};
这是现场工作 https://godbolt.org/#g:!((g:!((g:!((h:codeEditor,i:(filename:%271%27,fontScale:14,fontUsePx:%270%27,j:1,lang:c%2B%2B,selection:(endColumn:22,endLineNumber:20,positionColumn:22,positionLineNumber:20,selectionStartColumn:22,selectionStartLineNumber:20,startColumn:22,startLineNumber:20),source:%27%0Atemplate+%3Cclass+T%3E%0Aclass+A%7B%7D%3B%0A%0Atemplate+%3Cclass+C%3E%0Aconcept+A_+%3D+requires(C+c)+%7B%0A++++//+IILE,+that+only+binds+to+A%3C...%3E+specializations%0A++++//+Including+classes+derived+from+them%0A++++%5B%5D%3Ctypename+X%3E(A%3CX%3E%26)%7B%7D(c)%3B%0A%7D%3B%0A%0Atemplate+%3CA_+T%3E%0Aclass+B%7B%7D%3B%0A%0Aclass+X%7B%7D%3B%0Aclass+Y+:+public+A%3CX%3E+%7B%7D%3B%0A%0Aclass+Z+:+public+B%3CY%3E+%7B%7D%3B%0A%0A//+This+won!%27t+compile%0A//class+M+:+B%3CX%3E+%7B%7D%0A%0Aint+main()+%7B%0A++++%0A%7D%27),l:%275%27,n:%270%27,o:%27C%2B%2B+source+%231%27,t:%270%27)),k:50,l:%274%27,n:%270%27,o:%27%27,s:0,t:%270%27),(g:!((h:executor,i:(argsPanelShown:%271%27,compilationPanelShown:%270%27,compiler:g112,compilerOutShown:%270%27,execArgs:%27%27,execStdin:%27%27,fontScale:14,fontUsePx:%270%27,j:1,lang:c%2B%2B,libs:!(),options:%27-std%3Dc%2B%2B20+-pedantic-errors+-Wall+-Wextra+-Og%27,source:1,stdinPanelShown:%271%27,tree:%271%27,wrap:%271%27),l:%275%27,n:%270%27,o:%27Executor+x86-64+gcc+11.2+(C%2B%2B,+Editor+%231)%27,t:%270%27)),header:(),k:50,l:%274%27,n:%270%27,o:%27%27,s:0,t:%270%27)),l:%272%27,n:%270%27,o:%27%27,t:%270%27)),version:4.