Re: a hard problem about template specification
<wangjk@ihep.ac.cn> a ?crit dans le message de news:
d9a71be4-9b22-46be-8971-a6b3793e1759@u6g2000prc.googlegroups.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