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

[BUG] can not override a virtual function to pure virtual #1325

Open
Xeverous opened this issue Oct 22, 2024 · 1 comment
Open

[BUG] can not override a virtual function to pure virtual #1325

Xeverous opened this issue Oct 22, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@Xeverous
Copy link

Describe the bug
cppfront does not allow to override a function into pure virtual. Whether base type function is pure or not has no impact.

To Reproduce
sample code:

base: type =
{
    d: (virtual move this);
    f: (virtual this) -> int = 1;
}

derived: type =
{
    this: base = ();
    f: (override this) -> int;
}

expected result - something like this:

class base
{
public:
    virtual ~base() = default;
    virtual int f() { return 1; } 
};

class derived : public base
{
public:
    int f() override = 0;
};

actual result: main.cpp2(10,5): error: a function must have a body ('=' initializer), unless it is virtual (has a 'virtual this' parameter) or is defaultable (operator== or operator<=>)

Command lines

cppfront main.cpp2 -p, built from commit dc3758a

Additional context

Conflicting documentation - https://hsutter.github.io/cppfront/cpp2/types/ says:

A pure virtual function is a function with a virtual this or override this parameter and no body.

@Xeverous Xeverous added the bug Something isn't working label Oct 22, 2024
@gregmarr
Copy link
Contributor

Probably need to add a check for override here.

cppfront/source/sema.h

Lines 1636 to 1649 in dc3758a

// A nonvirtual and nondefaultable function must have an initializer
if (
n.is_function()
&& !n.is_virtual_function()
&& !n.is_defaultable_function()
&& !n.has_initializer()
)
{
handle_error(
n.position(),
"a function must have a body ('=' initializer), unless it is virtual (has a 'virtual this' parameter) or is defaultable (operator== or operator<=>)"
);
return false;
}

I wasn't even aware that this was a thing. I've never seen a pure virtual override, but I can see where it can be useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants