maliput / maliput/maliput

Error when using macro for default constructors and operators

Open
#491 0 comments 0 reactions 0 assignees View on GitHub
common
Dominant language
C++
Stars
16
Forks
5
PR merge metrics
No merged PRs in 30d

Description

### Context
Comes from https://github.com/ToyotaResearchInstitute/maliput_object/pull/7#discussion_r862071804

The macro `MALIPUT_DEFAULT_COPY_AND_MOVE_AND_ASSIGN`
performs a checking:
```cpp
/* Fails at compile-time if default-copy doesn't work. */ \
static void MALIPUT_COPYABLE_DEMAND_COPY_CAN_COMPILE() { \
(void)static_cast(&Classname::operator=); \
}
```

In some particular case, the static check fails in compile-time however I don't see why this is failing.
Consider the following snippet tried out in an online compiler.
https://coliru.stacked-crooked.com/a/44d6f75aa8224bb6

Click to see the snippet

```cpp
#include
#include

class MyTest {
public:

MyTest(const MyTest&) = default;
MyTest& operator=(const MyTest&) = default;
MyTest(MyTest&&) = default;
MyTest& operator=(MyTest&&) = default;
MyTest() = default;

static void CHECK_IF_IT_COMPILES() {
(void)static_cast(&MyTest::operator=);
}

const int my_int{2};
const std::string my_str{"hi"};
};

int main ()
{


MyTest my_test;
MyTest my_test_2 = my_test;
MyTest my_test_3{my_test};
std::cout << my_test_3.my_str << std::endl;

}
```



This fails with
```
main.cpp: In static member function 'static void MyTest::CHECK_IF_IT_COMPILES()':
main.cpp:14:77: error: use of deleted function 'MyTest& MyTest::operator=(const MyTest&)'
14 | (void)static_cast(&MyTest::operator=);
| ^
main.cpp:8:11: note: 'MyTest& MyTest::operator=(const MyTest&)' is implicitly deleted because the default definition would be ill-formed:
8 | MyTest& operator=(const MyTest&) = default;
```
**Only** when the private members are `const`

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by reproducing the compiler error from the issue's MyTest snippet and inspect the MALIPUT_DEFAULT_COPY_AND_MOVE_AND_ASSIGN macro, especially its static operator= check. Compare behavior with and without const members, then determine and document the intended fix or scope for the macro; the issue does not name a repository file or test.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.