Discussion:
Types in template functions.
(too old to reply)
Vladimir Grigoriev
2009-12-07 12:45:29 UTC
Permalink
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
Igor Tandetnik
2009-12-07 12:58:36 UTC
Permalink
Post by Vladimir Grigoriev
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
Vladimir Grigoriev
2009-12-07 13:06:38 UTC
Permalink
Thanks, Igor.
Only the question: cv in cv-qualified means const or volatile?

Vladimir Grigoriev
Post by Vladimir Grigoriev
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
Igor Tandetnik
2009-12-07 13:15:00 UTC
Permalink
Post by Vladimir Grigoriev
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
Loading...