Template Qualifier Removal Question

Hi,

I have a question relating to template metafunctions like remove_const that split a type from its qualifiers.

I was wondering where in Clang the decision to remove const (or another qualifier or pointer) from a type is made when using templates and then where it’s performed (the new type generated)? I originally thought the conversion might be in TreeTransform.h in a function like TransformQualifiedType but it doesn’t appear to be the case (I could be missing something however).

Sorry if the question is not entirely clear and thank you very much for your time and help!

Best Regards,
Andrew

Hi Andrew,

Hi,

I have a question relating to template metafunctions like remove_const that
split a type from its qualifiers.

I was wondering where in Clang the decision to remove const (or another
qualifier or pointer) from a type is made when using templates and then
where it's performed (the new type generated)?

Are you talking about std::remove_const (
https://en.cppreference.com/w/cpp/types/remove_cv ) ?

I have bad news: std::remove_const doesn't remove const from a type.
It merely creates another type.

From the "Possible implementation" :

template< class T > struct remove_const { typedef T type; };
template< class T > struct remove_const<const T> { typedef T type; };

remove_const<T> creates a new type ( remove_const<T>::type ) which is
exactly the same as T,
except if T is const, remove_const<T>::type isn't.

Csaba

Hi Csaba,

Thank you for the quick reply! That’s true it creates a new type, but I would like to know at the point of template instantiation where the typedef T type (from your example) is worked out from the original past in type within the compiler. If that’s a thing that occurs of course.

I’m unfortunately not entirely familiar with how it works so I could be wrong, but I imagine the T component needs to be deduced at some point and separated from the const component. So that you’d end up with an int instead of an const int for example.

Sorry if I’m not making sense and I apologize for phrasing my initial question quite badly. I’ll try to be a little more careful with my wording in the future.

Best Regards,

Andrew

The C++ specification gives rules for deducing template arguments from an "argument" type and a "parameter" type, where the argument type is (typically) a concrete type and the parameter type is specified in terms of some number of template parameters. You can find these rules in the section labeled [temp.deduct], but the basic idea is that you look for types that, if substituted for the template parameters, give exactly the argument type. So, e.g., if you have a parameter type `const T` and an argument type `const int`, the substitution `T = int` is the best match; but if the parameter type were `U`, the best substitution would be `U = const int`.

In clang, this rule is implemented in SemaTemplateDeduction.cpp. For the specific part about qualifiers, search that file for the use of `removeCVRQualifiers`.

John.

Hi John,

Thank you very much, that was exactly what I was looking for!

Best Regards,

Andrew