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

QEP 314: Relax prohibition against "auto" - exception for use of auto for variables initialized using casts #330

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

nyalldawson
Copy link
Contributor

This change relaxes the prohibition against auto to allow use of auto for variable types when the variable type is explicitly stated during its initialization as the result of a cast operation, such as dynamic_cast, static_cast, const_cast, reinterpret_cast, qobject_cast,
qgsgeometry_cast, or qgis::down_cast

Specifically:

// allowed, the pointer types are explicit during initialization:
auto markerSymbol = dynamic_cast< QgsMarkerSymbol* >( symbol );
auto vectorLayer = qobject_cast< QgsVectorLayer* >( layer );
if ( auto lineString = qgsgeometry_cast< const QgsLineString* >( geometry ) ) { ... }

// NOT allowed, the type is not explicit:
auto myLineStringPtr = downcastGeometryAsLineString( geometry );

Refs comment at #319 (comment)

@nyalldawson nyalldawson added Policy In Discussion QEPs currently in discussion stage labels Feb 23, 2025
@m-kuhn
Copy link
Member

m-kuhn commented Feb 24, 2025

Comparing the examples

case 1

auto markerSymbol = dynamic_cast< QgsMarkerSymbol* >( symbol );

vs.

QgsMarkerSymbol* markerSymbol = dynamic_cast< QgsMarkerSymbol* >( symbol );

case 2

auto vectorLayer = qobject_cast< QgsVectorLayer* >( layer );

vs.

QgsVectorLayer* vectorLayer = qobject_cast< QgsVectorLayer* >( layer );

case 3

if ( auto lineString = qgsgeometry_cast< const QgsLineString* >( geometry ) ) { ... }

vs.

if ( const QgsLineString* lineString = qgsgeometry_cast< const QgsLineString* >( geometry ) ) { ... }

I personally always prefer the explicit version over the auto version, it provides me with more information compared to the "auto" version and I also don't see auto remove a lot of clutter here.

@uclaros
Copy link
Contributor

uclaros commented Feb 24, 2025

The class names used in the examples are small enough to not make a big difference between explicit and auto.
When using longer classes though, the difference is also bigger:

const QgsVectorLayerUndoPassthroughCommandChangeGeometry *merge = dynamic_cast<const QgsVectorLayerUndoPassthroughCommandChangeGeometry *>( other );

vs

auto merge = dynamic_cast<const QgsVectorLayerUndoPassthroughCommandChangeGeometry *>( other );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
In Discussion QEPs currently in discussion stage Policy
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants