llvm / llvm/llvm-project

[clang-tidy] New check idea: suggest `using <base>::<base>;` to bring in base class constructors

Open
#190,881 4 comments 1 reaction 0 assignees View on GitHub
check-request clang-tidy
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

Say you're writing a derived class, and you want it to be constructible just like its base:
```cpp
struct Base {
Base(int a, int b);
};
```
One way to express that is by writing out a constructor with the same signature that delegates to the base constructor:
```cpp
struct Derived : Base {
Derived(int a, int b): Base(a, b) {}
};
```
But this can be expressed more concisely as:
```cpp
struct Derived : Base {
using Base::Base;
};
```
You can't selectively bring in a specific constructor, so this is only a behaviour-preserving transformation if the derived class duplicates all of the base's constructors. The constructors must also match in `explicit`-ness, `noexcept`-ness, and so on. There's a bit of wiggle room here though: if a base constructor is `noexcept` and the derived one isn't, that's probably just an oversight and we're okay to warn. If on the other hand the derived constructor *adds* noexcept, we probably shouldn't warn: chaning it to `using` would be weakening the noexcept guarantee.

This is a C++11 feature, so this would make sense as a `modernize` check. I could however see an argument for making it a `readability` check too.

This issue was prompted by [this comment here](https://github.com/llvm/llvm-project/pull/190302#discussion_r3031712816).

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.