C++20 support: consteval, concept/constraint, etc

647 views
Skip to first unread message

Sylvain Bellemare

unread,
Apr 18, 2023, 3:48:51 PM4/18/23
to cython-users
Hi,

I am using Cython to bind to a C++ codebase that makes use of new C++20 features, such as consteval and concept. I wonder where Cython is at in terms of supporting C++20 features.

I saw that v3.0 beta supports some C++20 features, but it does not seem to support the ones mentioned above.

I don't mind using a development (unstable) version, and could try helping adding missing features as well.

Thank you,
Sylvain

D Woods

unread,
Apr 18, 2023, 4:37:13 PM4/18/23
to cython-users
C++20 support is limited, and to be honest there's probably lower-hanging fruit to go for than consteval and concepts.

I'm struggling to see how you could do all that much with consteval and Cython (given that Python is fundamentally a runtime language). However, if you had a consteval function, I'd suggest starting by just telling Cython about the function signature without the consteval. It won't work in a lot of contexts of course (and Cython won't know this) but it'll likely let you use the functions in some places.

For a lot of concepts, again you wouldn't tell Cython about them - you'd simply tell Cython that a concepted function is a template function, and let C++ work out the concepts from what you pass to Cython. Where that falls down is if the return type varies with the concept, since Cython does need to know the return type (we haven't really got a good way of doing `auto` with Cython's code-generation model unfortunately).

For a "complicated" C++ interface I'm a big advocate of trying to write some simpler (C++) wrappers and exposing them to Cython. It doesn't always work of course, but C++ is a pretty complicated language and it's quite hard to make it all line up with Cython.

The 3.0 betas are very very close to the current development version, so there's no more unstable branch to look at (unless of course you spot a specific unmerged PR that you want to use...)

Sylvain Bellemare

unread,
Apr 19, 2023, 12:52:43 AM4/19/23
to cython-users
Thanks for the help! I'm totally new to Cython, but your recommendations make sense. 

Currently, occurrences of consteval and concept in the C++ code yield errors when building, e.g.:

/foo.hpp:55:12: error: unknown type name 'consteval'; did you mean 'constexpr'?
    static consteval inline Bar baz() { return Bar{0, 0}; }
           ^~~~~~~~~
           constexpr

I think I will look into implementing a thin C or C++ interface to that C++ codebase.

Thanks again!
Sylvain




D Woods

unread,
Apr 19, 2023, 3:06:18 AM4/19/23
to cython-users
On Wednesday, April 19, 2023 at 5:52:43 AM UTC+1 sbe...@gmail.com wrote:
Currently, occurrences of consteval and concept in the C++ code yield errors when building, e.g.:

/foo.hpp:55:12: error: unknown type name 'consteval'; did you mean 'constexpr'?
    static consteval inline Bar baz() { return Bar{0, 0}; }
           ^~~~~~~~~
           constexpr

That looks like an error from the C++ compiler rather than Cython (and from one of your headers rather than Cython-generated code). You probably just need to set the compiler to C++20 mode. If you're using setuptools+gcc you'd add "-std=c++20" to extra_compile_arg.
 
Reply all
Reply to author
Forward
0 new messages