CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jan 2009
    Location
    In a cave on Mars
    Posts
    30

    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"

  2. #2
    Join Date
    Nov 2006
    Location
    Essen, Germany
    Posts
    1,344

    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

  3. #3
    Join Date
    Jan 2009
    Location
    In a cave on Mars
    Posts
    30

    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"

  4. #4
    Join Date
    May 2007
    Location
    Scotland
    Posts
    1,164

    Re: explicitly specializing a templete function

    You are correct, but what is your reasoning?

  5. #5
    Join Date
    Jan 2009
    Location
    In a cave on Mars
    Posts
    30

    Re: explicitly specializing a templete function

    Well that is the way its defined, according to http://www.codersource.net/cpp_class...alization.html
    "What comes around, goes around"

  6. #6
    Join Date
    May 2007
    Location
    Scotland
    Posts
    1,164

    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
  •  





Click Here to Expand Forum to Full Width

Featured