-
January 12th, 2009, 06:04 AM
#1
explicitly specializing a templete function
A C++ developer wants to explicitly specialize the template function below for the char * type:
template <class T> void fn(T a){...}
Which of the following methods can the developer use to carry out this specialization?
A. void fn<char*>(T a){...}
B. void fn<char*>(char* a){...}
C. template <> void fn<char*>(char* a){...}
D. template <class T> void fn(char* a){...}
E. template <class T> void fn<char*>(T a){...}
Ok, I'm a little confused by what the question is asking. I believe its what to use char * for the template function given. In which case B is CORRECT and the rest are incorrect.
Can someone please confirm my thoughts? or point out what the question is really asking. Thx.
"What comes around, goes around"
-
January 12th, 2009, 06:13 AM
#2
Re: explicitly specializing a templete function
Most simple way is to let the compiler decide
If you have a book to read up template specialisation the correct solution will jump into your face.
What syntactical constraints does a templated function have to fulfill? Which of these functions meet the requirements?
And no, it´s not B.
- Guido
-
January 12th, 2009, 06:30 AM
#3
Re: explicitly specializing a templete function
I see, I didn't realise there was such a thing as template specialisation. Now that I've read it the answer is C.
"What comes around, goes around"
-
January 12th, 2009, 06:37 AM
#4
Re: explicitly specializing a templete function
You are correct, but what is your reasoning?
-
January 12th, 2009, 06:40 AM
#5
Re: explicitly specializing a templete function
"What comes around, goes around"
-
January 12th, 2009, 06:56 AM
#6
Re: explicitly specializing a templete function
Understanding the meaning of the syntax, and hence the syntactic constraints (which will reveal why C is correct) would help you more than merely adopting the 'because this web page says so' approach. I guess what I was really asking is, technically, why are A, B, D and E wrong, and why C is correct.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|