Whole Tomato Software Forums
Whole Tomato Software Forums
Main Site | Profile | Register | Active Topics | Members | Search | FAQ
User name:
Password:
Save Password
Forgot your password?

 All Forums
 Visual Assist
 Feature Requests
 Control const and & placement of Quick Fix
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

jschroedl
Tomato Guru

USA
107 Posts

Posted - Dec 19 2017 :  11:40:54 AM  Show Profile  Reply with Quote
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)

Edited by - jschroedl on Dec 19 2017 11:42:11 AM

feline
Whole Tomato Software

United Kingdom
19022 Posts

Posted - Dec 19 2017 :  1:53:35 PM  Show Profile  Reply with Quote
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.

zen is the art of being at one with the two'ness
Go to Top of Page

jschroedl
Tomato Guru

USA
107 Posts

Posted - Jan 10 2018 :  1:38:48 PM  Show Profile  Reply with Quote
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;
}

Go to Top of Page

feline
Whole Tomato Software

United Kingdom
19022 Posts

Posted - Jan 15 2018 :  4:58:59 PM  Show Profile  Reply with Quote
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.

zen is the art of being at one with the two'ness
Go to Top of Page

feline
Whole Tomato Software

United Kingdom
19022 Posts

Posted - Jan 23 2018 :  4:55:42 PM  Show Profile  Reply with Quote
Apologies for the delay in getting back to you about this. I have now put in a feature request for this:

case=113999

zen is the art of being at one with the two'ness
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
© 2023 Whole Tomato Software, LLC Go To Top Of Page
Snitz Forums 2000