From: Vladimir Grigoriev on
Let consider the function

template <typename T>

void check_const( T i )

{

++i;

}

If then it will be called such a way



check_const<const int>( 0 );

the compiler ( VC++ 2005 EE) issue the error

"error C3892: 'i' : you cannot assign to a variable that is const"

However if it will be called the following way

const int value = 0;

check_const( value );

the compiler does not issue the error.

Why does not the compiler issue the error in the second caase?



Vladimir Grigoriev


From: Igor Tandetnik on
Vladimir Grigoriev wrote:
> Let consider the function
>
> template <typename T>
> void check_const( T i )
> {
> ++i;
> }
>
> If then it will be called such a way
>
> check_const<const int>( 0 );
>
> the compiler ( VC++ 2005 EE) issue the error
>
> "error C3892: 'i' : you cannot assign to a variable that is const"
>
> However if it will be called the following way
>
> const int value = 0;
> check_const( value );
>
> the compiler does not issue the error.

14.8.2.1 Deducing template arguments from a function call
1 Template argument deduction is done by comparing each function template parameter type (call it P) with the type of the corresponding argument of the call (call it A) as described below.
2 If P is not a reference type:
....
- If A is a cv-qualified type, the top level cv-qualifiers of A’s type are ignored for type deduction.

So, T is deduced as int, not as const int.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not necessarily a good idea. It is hard to be sure where they are going to land, and it could be dangerous sitting under them as they fly overhead. -- RFC 1925
From: Vladimir Grigoriev on
Thanks, Igor.
Only the question: cv in cv-qualified means const or volatile?

Vladimir Grigoriev

"Igor Tandetnik" <itandetnik(a)mvps.org> wrote in message
news:%23e3SH0zdKHA.2596(a)TK2MSFTNGP04.phx.gbl...
Vladimir Grigoriev wrote:
> Let consider the function
>
> template <typename T>
> void check_const( T i )
> {
> ++i;
> }
>
> If then it will be called such a way
>
> check_const<const int>( 0 );
>
> the compiler ( VC++ 2005 EE) issue the error
>
> "error C3892: 'i' : you cannot assign to a variable that is const"
>
> However if it will be called the following way
>
> const int value = 0;
> check_const( value );
>
> the compiler does not issue the error.

14.8.2.1 Deducing template arguments from a function call
1 Template argument deduction is done by comparing each function template
parameter type (call it P) with the type of the corresponding argument of
the call (call it A) as described below.
2 If P is not a reference type:
....
- If A is a cv-qualified type, the top level cv-qualifiers of A�s type are
ignored for type deduction.

So, T is deduced as int, not as const int.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not necessarily
a good idea. It is hard to be sure where they are going to land, and it
could be dangerous sitting under them as they fly overhead. -- RFC 1925


From: Igor Tandetnik on
Vladimir Grigoriev wrote:
> Only the question: cv in cv-qualified means const or volatile?

Correct.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not necessarily a good idea. It is hard to be sure where they are going to land, and it could be dangerous sitting under them as they fly overhead. -- RFC 1925