I get an error message when I try to compile the code below:
======================== Error message ========================================
error C2664: 'f' : cannot convert parameter 1 from 'c<t1,t2>' to 'const c<t1,t2> &'
with
[
t1=char,
t2=int
]
and
[
t1=const char,
t2=int
]
===================================== Code ====================================
template< typename t1, typename t2 >
struct c
{
c( ) { }
operator c< t1 const, t2 >( ) const { return c< t1 const, t2 >( ); }
};

template< typename t2 >
void f( c< char const, t2 > const& )
{
}

int __cdecl main
(
)
{
c< char, int > C;
f( C );
}

Why is the conversion operator not used in this context?

Re: Conversion operator not callable? by msalters

msalters
Fri Jul 22 05:17:31 CDT 2005



BigMan schreef:
> I get an error message when I try to compile the code below:
> ======================== Error message
> error C2664: 'f' : cannot convert parameter 1 from 'c<t1,t2>' to
'const c<t1,t2> &'
> with [ t1=char, t2=int ]
> and [ t1=const char, t2=int ]
> ===================================== Code
> template< typename t1, typename t2 >
> struct c
> {
> c( ) { }
> operator c< t1 const, t2 >( ) const { return c< t1 const, t2 >( ); }
> };
>
> template< typename t2 >
> void f( c< char const, t2 > const& )
> {
> }
>
> int main ( )
> {
> c< char, int > C;
> f( C );
> }
>
> Why is the conversion operator not used in this context?

Because f is a template. That means the compiler has a theoretically
infinte set of overloads f<T>. Without templates, the set is finite
and the compiler can determine per overload what conversion sequence
is needed. Clearly, that algorithm wouldn't work for infinite sets.

Therefore, the C++ standard says that you must have an exact match
for the argument of foo. In the absence of conversions, the compiler
can exactly deduce the template arguments from the provided argument.

In you example, the compiler simply can't find a t2 for which
c<char const, t2> equals c<char,int>

HTH,
Michiel Salters


Re: Conversion operator not callable? by BigMan

BigMan
Tue Jul 26 00:27:56 CDT 2005


"msalters" <Michiel.Salters@logicacmg.com> wrote in message news:1122027451.766331.265470@z14g2000cwz.googlegroups.com...
>
>
> BigMan schreef:
>> I get an error message when I try to compile the code below:
>> ======================== Error message
> > error C2664: 'f' : cannot convert parameter 1 from 'c<t1,t2>' to
> 'const c<t1,t2> &'
>> with [ t1=char, t2=int ]
>> and [ t1=const char, t2=int ]
>> ===================================== Code
>> template< typename t1, typename t2 >
>> struct c
>> {
>> c( ) { }
>> operator c< t1 const, t2 >( ) const { return c< t1 const, t2 >( ); }
>> };
>>
>> template< typename t2 >
>> void f( c< char const, t2 > const& )
>> {
>> }
>>
>> int main ( )
>> {
>> c< char, int > C;
>> f( C );
>> }
>>
>> Why is the conversion operator not used in this context?
>
> Because f is a template. That means the compiler has a theoretically
> infinte set of overloads f<T>. Without templates, the set is finite
> and the compiler can determine per overload what conversion sequence
> is needed. Clearly, that algorithm wouldn't work for infinite sets.
>
> Therefore, the C++ standard says that you must have an exact match
> for the argument of foo.

Where does it say so?

> In the absence of conversions, the compiler
> can exactly deduce the template arguments from the provided argument.
>
> In you example, the compiler simply can't find a t2 for which
> c<char const, t2> equals c<char,int>
>
> HTH,
> Michiel Salters
>

Re: Conversion operator not callable? by Tom

Tom
Tue Jul 26 04:13:44 CDT 2005

BigMan wrote:

>> Because f is a template. That means the compiler has a theoretically
>> infinte set of overloads f<T>. Without templates, the set is finite
>> and the compiler can determine per overload what conversion sequence
>> is needed. Clearly, that algorithm wouldn't work for infinite sets.
>>
>> Therefore, the C++ standard says that you must have an exact match
>> for the argument of foo.
>
>
> Where does it say so?

14.8.2.1.

Tom