Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   a hard problem about template specification (http://www.velocityreviews.com/forums/t621273-a-hard-problem-about-template-specification.html)

wangjk@ihep.ac.cn 06-20-2008 01:55 AM

a hard problem about template specification
 
Hi,
I know there are many experienced C++ experts be here, i have a
puzzle :

(1) template <typename L, typename R, bool rL = false, bool rR =
flase > class A{.......};

(2) template <typename L, typename R> class
A<L,R,true,true>{......};

and there is a partial specification operator :
(3) template < >A<L,R>::operator()(int){......};


int main(){
......
return (new A<int,int,true,true>())->(5);
...

}


in the main function, in the return sentence ......->(5);, we called
the operator(),
I think it should call class (2)'s or it's base class's operator (),
but the result is that it called the operator in (3).

Who can tell me some? thanks!


Eric Pruneau 06-20-2008 02:53 AM

Re: a hard problem about template specification
 

<wangjk@ihep.ac.cn> a écrit dans le message de news:
d9a71be4-9b22-46be-8971-a6b3793e1759...oglegroups.com...
> Hi,
> I know there are many experienced C++ experts be here, i have a
> puzzle :
>
> (1) template <typename L, typename R, bool rL = false, bool rR =
> flase > class A{.......};
>
> (2) template <typename L, typename R> class
> A<L,R,true,true>{......};
>
> and there is a partial specification operator :
> (3) template < >A<L,R>::operator()(int){......};
>
>
> int main(){
> ......
> return (new A<int,int,true,true>())->(5);
> ...
>
> }
>
>
> in the main function, in the return sentence ......->(5);, we called
> the operator(),
> I think it should call class (2)'s or it's base class's operator (),
> but the result is that it called the operator in (3).
>
> Who can tell me some? thanks!
>


What is sure is that
new A<int,int,true,true>()
calls the constructor of (2).

Well (3) is not very clear to me... you should paste a more detailed code.
But what I understand is that the specialization of A (case 2) has an
operator() taking an int (looks like there is no return type!).

so after creating A you call operator() with 5 as the argument, so it calls
(3)


here is what I think you wanna do

template<typename T, typename L, bool b1=false, bool b2=false>
struct A
{
A() {cout<<"1\n";}
};

template <typename T, typename L>
struct A<T, L,true,true>
{
A(){cout<<"2\n";}
int operator()(int i);
};

template<typename T, typename L>
A<T,L,true,true>::operator()(int i)
{
cout<< i<<endl;
}

int main()
{
return (new A<s,s,true,true>)->operator()(5);
}

this print
2
5


---------------

Eric Pruneau



Road.Tang 06-20-2008 05:36 AM

Re: a hard problem about template specification
 
On Jun 20, 9:55 am, wan...@ihep.ac.cn wrote:
> Hi,
> I know there are many experienced C++ experts be here, i have a
> puzzle :
>
> (1) template <typename L, typename R, bool rL = false, bool rR =
> flase > class A{.......};
>
> (2) template <typename L, typename R> class
> A<L,R,true,true>{......};
>
> and there is a partial specification operator :
> (3) template < >A<L,R>::operator()(int){......};


this is syntax error.

>
> int main(){
> ......
> return (new A<int,int,true,true>())->(5);
> ...
>
> }
>
> in the main function, in the return sentence ......->(5);, we called
> the operator(),
> I think it should call class (2)'s or it's base class's operator (),
> but the result is that it called the operator in (3).
>


basically, it will call the best match and specific one.
well, the result indicates your (3) is best match and the most
specific operator for A<int, int, true, true>..

but you know, both compile and i doesn't understand the syntax error
code.


-roadt

> Who can tell me some? thanks!



Road.Tang 06-20-2008 05:38 AM

Re: a hard problem about template specification
 
On Jun 20, 9:55 am, wan...@ihep.ac.cn wrote:
> Hi,
> I know there are many experienced C++ experts be here, i have a
> puzzle :
>
> (1) template <typename L, typename R, bool rL = false, bool rR =
> flase > class A{.......};
>
> (2) template <typename L, typename R> class
> A<L,R,true,true>{......};
>
> and there is a partial specification operator :
> (3) template < >A<L,R>::operator()(int){......};


this is syntax error.

>
> int main(){
> ......
> return (new A<int,int,true,true>())->(5);
> ...
>
> }
>
> in the main function, in the return sentence ......->(5);, we called
> the operator(),
> I think it should call class (2)'s or it's base class's operator (),
> but the result is that it called the operator in (3).
>


basically, it will call the best match and specific one.
well, the result indicates your (3) is best match and the most
specific operator for A<int, int, true, true>..

but you know, both compile and i doesn't understand the syntax error
code.


-roadt

> Who can tell me some? thanks!



wangjk@ihep.ac.cn 06-20-2008 07:45 AM

Re: a hard problem about template specification
 
Hi, Eric

Thanks for your suggestion very much! I paste it again:


(1)
template<typename T, typename L, bool b1=false, bool b2=false>
struct A
{
A() {cout<<"1\n";}
};


(2)
template <typename T, typename L>
struct A<T, L,true,true>
{
A(){cout<<"2\n";}
int operator()(int i);

};



(3)
template<typename T, typename L>
A<T,L>::operator()(int i)
{
cout<< i<<endl;

}




int main()
{
return (new A<s,s,true,true>)->operator()(5);


}


this print
2
5


In my opinion, I think the sentence (new A<s,s,true,true>)->operator()
(5) will
call the operator() in (2), but it call the explicit version in (3). I
do not
know why?





wangjk@ihep.ac.cn 06-20-2008 09:32 AM

Re: a hard problem about template specification
 

Hi, Eric and roadt, thanks your good suggestion!

I paste it again.

(1)
template<typename L, typename R, bool b1=false, bool b2=false>
struct A
{
A() {cout<<"1\n";}

};

(2)
template <typename L, typename R>
struct A<L, R, true, true>
{
A(){cout<<"2\n";}
int operator()(int i);

};

(3)
template< >
A<int,int>::operator()(int i)
{
cout<< i<<endl;

}


int main()
{
return (new A<int,int,true,true>)->operator()(5);


}


this print
2
5


I think the setence
(new A<int,int,true,true>)->operator()(5)

will call the operator()(int) of (2).
But it call the operator()(int) of (3), it is so strange.
Because there are 2 dafault bool parameters in template(1).
So I think the operator of (3) is equal to
template< > A<int,int, false, false>::operator()(int),
of course the (new A<int,int,true,true>)->operator()(5)
not match it.


Marcel Müller 06-20-2008 12:08 PM

Re: a hard problem about template specification
 
Hi,

wangjk@ihep.ac.cn wrote:
> I paste it again.
>
> (1)
> template<typename L, typename R, bool b1=false, bool b2=false>
> struct A
> {
> A() {cout<<"1\n";}
>
> };
>
> (2)
> template <typename L, typename R>
> struct A<L, R, true, true>
> {
> A(){cout<<"2\n";}
> int operator()(int i);
>
> };
>
> (3)
> template< >
> A<int,int>::operator()(int i)
> {
> cout<< i<<endl;
>
> }
>
>
> int main()
> {
> return (new A<int,int,true,true>)->operator()(5);
>
>
> }


Your code still does not compile, since A<int,int,false,false> does not
have operator()(int) defined. Furthermore this operator has no return
value. Both are errors.

So who came up with this output???

> this print
> 2
> 5




Marcel

Eric Pruneau 06-20-2008 05:17 PM

Re: a hard problem about template specification
 

"Marcel Müller" <news.5.maazl@spamgourmet.org> a écrit dans le message de
news: 485b9dba$0$6601$9b4e6d93@newsspool2.arcor-online.net...
> Hi,
>
> wangjk@ihep.ac.cn wrote:
>> I paste it again.
>>
>> (1)
>> template<typename L, typename R, bool b1=false, bool b2=false>
>> struct A
>> {
>> A() {cout<<"1\n";}
>>
>> };
>>
>> (2)
>> template <typename L, typename R>
>> struct A<L, R, true, true>
>> {
>> A(){cout<<"2\n";}
>> int operator()(int i);
>>
>> };
>>
>> (3)
>> template< >
>> A<int,int>::operator()(int i)
>> {
>> cout<< i<<endl;
>>
>> }
>>
>>
>> int main()
>> {
>> return (new A<int,int,true,true>)->operator()(5);
>>
>>
>> }

>
> Your code still does not compile, since A<int,int,false,false> does not
> have operator()(int) defined. Furthermore this operator has no return
> value. Both are errors.


this example compile and run fine with the intel compiler

(1) does not need to have an operator(). the implementation of the
specialization does not need to be related in any way to the generic
definition.


> So who came up with this output???


me ( or at least my computer...)

>
>> this print
>> 2
>> 5

>
>
>
> Marcel


---------------

Eric Pruneau



Eric Pruneau 06-20-2008 05:21 PM

Re: a hard problem about template specification
 

<wangjk@ihep.ac.cn> a écrit dans le message de news:
08235181-2c5c-46ff-a7c7-5b6312e00a54...oglegroups.com...
>
> Hi, Eric and roadt, thanks your good suggestion!
>
> I paste it again.
>
> (1)
> template<typename L, typename R, bool b1=false, bool b2=false>
> struct A
> {
> A() {cout<<"1\n";}
>
> };
>
> (2)
> template <typename L, typename R>
> struct A<L, R, true, true>
> {
> A(){cout<<"2\n";}
> int operator()(int i);
>
> };
>
> (3)
> template< >
> A<int,int>::operator()(int i)
> {
> cout<< i<<endl;
>
> }
>
>
> int main()
> {
> return (new A<int,int,true,true>)->operator()(5);
>
>
> }
>
>
> this print
> 2
> 5
>
>
> I think the setence
> (new A<int,int,true,true>)->operator()(5)
>
> will call the operator()(int) of (2).


Thats is exactly what it does

> But it call the operator()(int) of (3), it is so strange.


wait, (3) should be

template< > A<int,int,true,true>::operator()(int i) { ... }
not
template< > A<int,int>::operator()(int i) { ... }

the latter does not compile

-------------------

Eric Pruneau


> Because there are 2 dafault bool parameters in template(1).
> So I think the operator of (3) is equal to
> template< > A<int,int, false, false>::operator()(int),
> of course the (new A<int,int,true,true>)->operator()(5)
> not match it.
>




Eric Pruneau 06-20-2008 05:25 PM

Re: a hard problem about template specification
 

"Eric Pruneau" <eric.pruneau@cgocable.ca> a écrit dans le message de news:
9DR6k.8917$Ec.3905@read2.cgocable.net...
>
> "Marcel Müller" <news.5.maazl@spamgourmet.org> a écrit dans le message de
> news: 485b9dba$0$6601$9b4e6d93@newsspool2.arcor-online.net...
>> Hi,
>>
>> wangjk@ihep.ac.cn wrote:
>>> I paste it again.
>>>
>>> (1)
>>> template<typename L, typename R, bool b1=false, bool b2=false>
>>> struct A
>>> {
>>> A() {cout<<"1\n";}
>>>
>>> };
>>>
>>> (2)
>>> template <typename L, typename R>
>>> struct A<L, R, true, true>
>>> {
>>> A(){cout<<"2\n";}
>>> int operator()(int i);
>>>
>>> };
>>>
>>> (3)
>>> template< >
>>> A<int,int>::operator()(int i)
>>> {
>>> cout<< i<<endl;
>>>
>>> }
>>>
>>>
>>> int main()
>>> {
>>> return (new A<int,int,true,true>)->operator()(5);
>>>
>>>
>>> }

>>
>> Your code still does not compile, since A<int,int,false,false> does not
>> have operator()(int) defined. Furthermore this operator has no return
>> value. Both are errors.

>
> this example compile and run fine with the intel compiler


Wait, I wrote
template< > A<int,in,true,truet>::operator()(int i) { ...}

for (3)

I agree that like if you try to compile the example like it is, it will not
compile.

> (1) does not need to have an operator(). the implementation of the
> specialization does not need to be related in any way to the generic
> definition.
>
>
>> So who came up with this output???

>
> me ( or at least my computer...)
>
>>
>>> this print
>>> 2
>>> 5

>>
>>
>>
>> Marcel

>
> ---------------
>
> Eric Pruneau
>





All times are GMT. The time now is 05:21 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.