Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C++: classes in classes, cimport not consistent #3074

Open
gerion0 opened this issue Aug 9, 2019 · 1 comment

Comments

@gerion0
Copy link

commented Aug 9, 2019

Consider this minimal example:

foo.h

#pragma once

#include <iostream>

struct Foobar {
        struct LongName {
                void foo() {
                        std::cout << "foo" << std::endl;
                }
        };

        static LongName get() {
                return Foobar::LongName();
        }

};

foo.pxd

#cython: language_level=3

cdef extern from "foo.h":
    cdef cppclass Foobar:
        cppclass LongName:
            void foo()

        @staticmethod
        LongName get()

foo.pyx

from foo cimport Foobar

def my_func():
    cdef Foobar.LongName bla
    bla = Foobar.get()
    bla.foo()

This works so far. However, this does not work:

foo1.pyx

cimport foo

def my_func():
    cdef foo.Foobar.LongName bla
    bla = foo.Foobar.get()
    bla.foo()

foo2.pyx

from foo cimport Foobar
from foo cimport Foobar.LongName as LongName
# alternative, also not working:
# from foo.Foobar cimport LongName as LongName

def my_func():
    cdef LongName bla
    bla = Foobar.get()
    bla.foo()

While this works:

foo3.pyx

from foo cimport Foobar

ctypedef Foobar.LongName LongName

def my_func():
    cdef LongName bla
    bla = Foobar.get()
    bla.foo()

For me, this feels like C++ class in C++ class is not consistent. Is it possible to support also the long nested name (foo.Foobar.LongName)?

@scoder

This comment has been minimized.

Copy link
Contributor

commented Aug 14, 2019

A cimport from a class cannot work (only from modules, as in Python), but the other examples look reasonable to me. PR welcome to make them work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.