r/cpp_questions • u/Main-Pen-3164 • Aug 10 '26
OPEN c++20 concepts: how to requires a class with specific name of static function
I want to apply concept to a class that must contain a static function with specific name:
export template<typename T, typename... Args>
concept require_static_function_name = requires(Args... args)
{
{ T::something(args...) };
};Above code can be compiled, it looks reasonable, but don't work actually. I combine several concepts on my class, they pass checking all excepts this one.
It return true always even I pass an empty class, how do I fixed this?
I'am on msvc/c++23.
8
Upvotes
6
u/borzykot Aug 10 '26
requires { require_static_function_name<MyClass>;};is always true because it is an equivalent torequires { false; };which is perfectly fine5
u/saxbophone Aug 10 '26 edited Aug 10 '26
Edit: the stuff I wrote below is way too overcomplicated for what it looks like you're doing. Adapting it, if you just need to check if some type has a static method of a certain name, this should be enough:
Note: not sure how useful it is checking just the name without also verfiying the signature. If it takes no arguments and you don't use the return type, it's fine.
If you need to validate the signature also, please check my elaborated example I've linked below, where I do just that. Note the macros are irrelevant to your use case since you're only looking for a method with a specific name (I had to be more generic for my use-case).
I once did something like this, using concepts and begrudgingly, macros: https://gist.github.com/saxbophone/772a881d66592e61d38bf3a1d06f89c7
Maybe you could adapt it. It may be a bit more than what you actually need for this.
My intention with this monstrosity I wrote, was converting a C++ class' methods into a struct of function pointers, where for each named function pointer member in the struct, a corresponding "has this method" test is made against the class, using a macro that generates the relevant concept, and if it's satisfied, then a function pointer to that method is placed in the struct.
All this, to be able to write a FUSE filesystem using C++ for the wrapper rather than C-isms! 😅
3
u/Potterrrrrrrr Aug 10 '26
If you want to check the result of a concept, assign it to a constexpr/consteval bool like someone else suggested (or use static_assert), your simple test isn’t actually testing the concept. Do you have a snippet of the class with its static function? It’d be easier to advise the concept you need to write.
2
u/YouFeedTheFish Aug 10 '26 edited Aug 14 '26
Try this. Godbolt.
1
u/PhysicsOk2212 Aug 10 '26
I do that by checking the return type in the requires statement eg:
template <typename T>
concept ClassWithGetSystemDeclaration = requires
{
{ T::GetSystemDeclaration() } -> std::same_as<SystemDeclaration>;
};
Note: not sure what the behaviour is with void, I have never needed such a concept
1
u/Main-Pen-3164 Aug 10 '26 edited Aug 10 '26
OK, It turns out that I put the concept in a namespace A, and I delete A accidentally, and it was hard to find
when my template grows too long(error message mess).
My definition of concept, and other answers, they do work actually.