Skip to content

[BUG] Emitted code relies on implicit move and copy constructors, but move and copy constructors are marked explicit #375

Closed
@cpp-niel

Description

@cpp-niel

Describe the bug
cppfront is emitting the explicit specifier on all single argument constructors including move and copy constructors. At the same time implicit move and copy are being used by code that is emitted - for instance - when returning named values from functions.

To Reproduce
Steps to reproduce the behavior:

  1. Sample code - distilled down to minimal essentials please
    Here is a Compiler Explorer link demonstrating the problem: https://godbolt.org/z/x8T4ec7Wd
    Note how foo's move and copy constructors are explicit, but the call to get_foo() in main would need at least an implicit copy constructor.
  2. Command lines including which C++ compiler you are using
    Calling cppfront with -p and compiling the result with gcc-12
  3. Expected result - what you expected to happen
    I would expect copy and move constructors to not be explicit
  4. Actual result/error6.
    Copy and move constructors are marked explicit

Additional context
I have resolved this issue locally by only emitting the explicit specifier if the constructor does not have a that:

diff --git a/source/cppfront.cpp b/source/cppfront.cpp
index 570c078..e4e2a04 100644
--- a/source/cppfront.cpp
+++ b/source/cppfront.cpp
@@ -5139,6 +5139,7 @@ public:
                         break;default:
                             if (
                                 func->is_constructor()
+                                && !func->is_constructor_with_that()
                                 && func->parameters->ssize() == 2
                                 && generating_assignment_from != &n
                                 )

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions