Whole Tomato Software Forums
Whole Tomato Software Forums
Main Site | Profile | Register | Active Topics | Members | Search | FAQ
 All Forums
 Visual Assist
 Feature Requests
 Control const and & placement of Quick Fix

You must be registered to post a reply.
Click here to register.

Screensize:
UserName:
Password:
Format: BoldItalicizeUnderlineStrikethrough Align leftCenterAlign right Insert horizontal ruleUpload and insert imageInsert hyperlinkInsert email addressInsert codeInsert quoted textInsert listInsert Emoji
   
Message:

Forum code is on.
Html is off.

 
Check to subscribe to this topic.
   

T O P I C    R E V I E W
jschroedl Posted - Dec 19 2017 : 11:40:54 AM
I'd really love to have control over the location of 'const' and the '&' placed by the new code inspection quick fix: "Range-based loop variable can be a const reference"

for (auto x : container)


becomes

for (const auto& x : container)


but I prefer the "const always on the right-side" mentality so I'd want this...

for (auto const & x : container)
4   L A T E S T    R E P L I E S    (Newest First)
feline Posted - Jan 23 2018 : 4:55:42 PM
Apologies for the delay in getting back to you about this. I have now put in a feature request for this:

case=113999
feline Posted - Jan 15 2018 : 4:58:59 PM
This makes sense. I had not really realised this about the const keyword, I just have a general sense of how const works, which does not always work for more complex situations.

I just want to make sure I have test cases for the different ways Code Inspection can insert a const before putting in a request for this, to make sure we have a good sense of what we want to cover.
jschroedl Posted - Jan 10 2018 : 1:38:48 PM
quote:
Doesn't the placement of const effect its meaning?


Yes, it does but auto can be augmented with const and &.

ex.

int foo();

auto val = foo();         // val is int
auto const val = foo();   // val is int const
auto const & val = foo(); // val is int const & (aka const int &)



const always applies to the type on it's left (except for the exception that you can put it on the left of the first type).

We prefer to always have the const on the right of what it applies to so there's no ambiguity reading.

ex. we prefer: int const * const ptr;
over: const int * const ptr;

This is the placement control we want with the Quick Fix.
ie. const always goes on the right of auto

A test case to make the point if it helps...

#include <type_traits>

int foo() { return 42; }

int main(int, char**)
{
	auto val = foo();
	auto const cval = foo();
	auto const & acr = foo();
	const auto & car = foo();
	
	static_assert(std::is_same<int, decltype(val)>::value);
	static_assert(std::is_same<int const, decltype(cval)>::value);
	static_assert(std::is_same<int const &, decltype(acr)>::value);
	static_assert(std::is_same<int const &, decltype(car)>::value);

	acr = 21; // error C3892: 'acr': you cannot assign to a variable that is const
	
	return 0;
}

feline Posted - Dec 19 2017 : 1:53:35 PM
Doesn't the placement of const effect its meaning? I know it does in some situations, but I am not sure with auto, since auto tells the compiler to work out the fully type for the variable.

© 2023 Whole Tomato Software, LLC Go To Top Of Page
Snitz Forums 2000