a hard problem about template specification

W

wangjk

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>::eek:perator()(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!
 
E

Eric Pruneau

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>::eek:perator()(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>::eek:perator()(int i)
{
cout<< i<<endl;
}

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

this print
2
5
 
R

Road.Tang

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>::eek:perator()(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
 
R

Road.Tang

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>::eek:perator()(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
 
W

wangjk

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>::eek:perator()(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?
 
W

wangjk

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>::eek:perator()(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>::eek:perator()(int),
of course the (new A<int,int,true,true>)->operator()(5)
not match it.
 
M

Marcel Müller

Hi,

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>::eek:perator()(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
 
E

Eric Pruneau

Marcel Müller said:
Hi,



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...)
 
E

Eric Pruneau

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>::eek:perator()(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>::eek:perator()(int i) { ... }
not
template< > A<int,int>::eek:perator()(int i) { ... }

the latter does not compile
 
E

Eric Pruneau

Eric Pruneau said:
this example compile and run fine with the intel compiler

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

for (3)

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

Eric Pruneau

Eric Pruneau said:
Wait, I wrote
template< > A<int,in,true,truet>::eek:perator()(int i) { ...}

for (3)

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

I meant will not link since operator() of (2) is not defined
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top