473,396 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 Rclass
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!

Jun 27 '08 #1
10 1290

<wa****@ihep.ac.cna écrit dans le message de news:
d9**********************************...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 Rclass
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
Jun 27 '08 #2
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 Rclass
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!
Jun 27 '08 #3
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 Rclass
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!
Jun 27 '08 #4
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?


Jun 27 '08 #5

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.

Jun 27 '08 #6
Hi,

wa****@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,falsedoes 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
Jun 27 '08 #7

"Marcel Müller" <ne**********@spamgourmet.orga écrit dans le message de
news: 48**********************@newsspool2.arcor-online.net...
Hi,

wa****@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,falsedoes 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
Jun 27 '08 #8

<wa****@ihep.ac.cna écrit dans le message de news:
08**********************************...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.

Jun 27 '08 #9

"Eric Pruneau" <er**********@cgocable.caa écrit dans le message de news:
9D****************@read2.cgocable.net...
>
"Marcel Müller" <ne**********@spamgourmet.orga écrit dans le message de
news: 48**********************@newsspool2.arcor-online.net...
>Hi,

wa****@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,falsedoes 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

Jun 27 '08 #10

"Eric Pruneau" <er**********@cgocable.caa écrit dans le message de news:
bK****************@read1.cgocable.net...
>
"Eric Pruneau" <er**********@cgocable.caa écrit dans le message de news:
9D****************@read2.cgocable.net...
>>
"Marcel Müller" <ne**********@spamgourmet.orga écrit dans le message de
news: 48**********************@newsspool2.arcor-online.net...
>>Hi,

wa****@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,falsedoes 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.
I meant will not link since operator() of (2) is not defined
Jun 27 '08 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Joachim Spoerhase | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, I am a XSLT-beginner and i read the XSLT-recommendation of the W3C through. But I did'nt really understand section 5.5 of the latest...
5
by: Suzanne Vogel | last post by:
Is it possible to store a pointer to a template function? eg, template<class T> fooFunc() {...} fptr = fooFunc; // <-- I couldn't find any info here, not even in the forums:...
5
by: Brandon Mitchell | last post by:
I have read that it is impossible to define a templated class outside of the specification file, due to a limitation in gcc's linker. I'm using gcc 3.2.3 (no switches) and would like to seperate...
2
by: Steven T. Hatton | last post by:
I don't really understand the details of what happens when a compiler inlines a given function. I know that basically it's supposed to cause the function to be placed directly on the stack per...
4
by: Tony Johansson | last post by:
Hello experts! I'm reading a book about C++ and there is something about inline that the book says that is unclear for me. The book says the following "Because inline functions are expanded at...
8
by: Adam Louis | last post by:
I would like help resolving this problem. I'm a novice who's been hired to query a hospital database and extract useful information, available to me only in a dynamically generated, downloadable...
9
by: Marek Vondrak | last post by:
Hello. I have written the following program and am curious why it prints "1" "2". What are the exact effects of explicitly providing function template parameters at the call? Is the second...
9
by: vilarneto | last post by:
Hello everyone, Today I started to use template specializations in a project and suddenly faced a curious problem. Following is a complete example that shows the situation: ---------- ...
1
by: Ed | last post by:
Hi, guys, I declare a template method in one template class in one library. Compiling is OK, but link is not OK. Header File is: <code> template <typename P = float> class TESTLIB_API Linear...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.