Content-Length: 585641 | pFad | https://github.com/cpplint/cpplint/pull/227

ad Don't complain about `explicit` no-arg constructors. by markww · Pull Request #227 · cpplint/cpplint · GitHub
Skip to content

Don't complain about explicit no-arg constructors. - #227

Merged
aaronliu0130 merged 67 commits into
cpplint:developfrom
markww:develop
Apr 23, 2024
Merged

Don't complain about explicit no-arg constructors.#227
aaronliu0130 merged 67 commits into
cpplint:developfrom
markww:develop

Conversation

@markww

@markww markww commented Aug 28, 2023

Copy link
Copy Markdown
Contributor

Previously, cpplint has complained when a no-argument constructor is marked explicit. This is bad for two reasons:

  1. As of C++11, converting constructors can be called with any number of arguments including 0. Therefore, this check is wrong. (see https://en.cppreference.com/w/cpp/language/converting_constructor )

  2. Even in pre-C++-11 code, it is good defensive programming practice to mark all constructors explicit. Marking a constructor that cannot be a conversion explicit has no ill effect, but if someone alters it to be a possible implicit conversion, it defends you against them forgetting to add explicit.

Georg Piewald and others added 2 commits February 4, 2019 18:07
In the situation, where one git/svn/hg repository is located inside the
directory tree of another, the search incorrectly selects the outer one
instead of the inner one.
@markww

markww commented Aug 28, 2023

Copy link
Copy Markdown
Contributor Author

This failing check doesn't seem to be caused by my changes. Any guidance on how to get them to pass?

Previously, cpplint has complained when a no-argument constructor is
marked `explicit`. This is bad for two reasons:

1. As of C++11, converting constructors can be called with any number of
   arguments including 0. Therefore, this check is wrong.
  (see
https://en.cppreference.com/w/cpp/language/converting_constructor )

2. Even in pre-C++-11 code, it is good defensive programming practice to
   mark all constructors `explicit`. Marking a constructor that cannot
   be a conversion `explicit` has no ill effect, but if someone alters
  it to be a possible implicit conversion, it defends you against them
  forgetting to add `explicit`.
aaronliu0130 and others added 21 commits February 4, 2024 22:38
Most blocked features have been removed, the c++tr1 and c++14 categories have been removed, and the c++17 category has been added. Same for relevant tests

1. The only blocked features I can see in the specification that haven't been blocked elsewhere are headers
2. Most of these blocked features aren't blocked in the style guide
2.1: A lot of these unwarrantedly blocked features, like ratio, are also very useful
2.2: TR1 is super old and we shouldn't be thinking about it anyways
3. There aren't enough features we need to block to warrant separate functions for each C++ standard
The verbose pytest output is quite annoying to read in a CI, and using a timeout solves the issue of knowing which tests hang.

Also included: tox.ini now installs the extra [dev], so we only need to make changes in one place now.
A chromium sample has been updated to a version that uses C++17 stuff

READMEs of samples have been clarified regarding licensing

Two licenses have also been updated

Our setup.py now includes a BSD classifier for PyPI
Move usage to its own section and provide rudimentary instructions to
defuse error caused by external package managers
Follow-up to cpplint#217, another try to fix cpplint#27. This stops IWYU on erring on tokens with namespace prefixes that aren't std.
This change should preserve backward compatibility while preventing false positives for common names in other namespaces.
@aaronliu0130

Copy link
Copy Markdown
Member

Note that the only change is c607830, I just merged to see if CI passes. I think that was a bad idea and I'll just run tox locally next time...

"Implicit conversions can sometimes be necessary and appropriate for types that are designed to be interchangeable, for example when objects of two types are just different representations of the same underlying value. In that case, contact your project leads to request a waiver of this rule."

@aaronliu0130 aaronliu0130 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems okay, but I'm not so experienced in that field. Is there any plausible reason you could think of for never marking zero-param constructors as explicit?

@aaronliu0130
aaronliu0130 requested review from jayvdb and tkruse March 31, 2024 16:01
@aaronliu0130 aaronliu0130 changed the title Remove diagnostic complaining about explicit no-arg constructors. Don't complain about explicit no-arg constructors. Apr 23, 2024
@aaronliu0130
aaronliu0130 merged commit ab7497b into cpplint:develop Apr 23, 2024
@geoffviola

Copy link
Copy Markdown
Contributor

The Google style guide neither recommends nor forbids it.

https://google.github.io/styleguide/cppguide.html#Implicit_Conversions

Some C++ experts recommend it.

Every other constructor (even the zero-argument constructor!) should be explicit or have a very well-understood domain-specific reason why not. Example: string(const char*).

https://quuxplusone.github.io/blog/2023/04/08/most-ctors-should-be-explicit/

reverses the Cpp1 default, where constructors are not "explicit" by default, and you have to write "explicit" to make them explicit.

https://hsutter.github.io/cppfront/cpp2/types/#implicit-controlling-conversion-functions

There is probably room for a tool to warn against not having explicit for non-move/copy/initializer list constructors. But the need seems small.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants









ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: https://github.com/cpplint/cpplint/pull/227

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy