CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Question Learning templates

    We've got a colleague in the office whom we all here think is a very good c++ programmer - with the exception of templates. He can't seem to get his head around them no matter how much we try to explain to him. We have copies of 'c++ Templates The Complete Guide' and 'Advanced c++ metaprogramming' books which he's tried to understand and failed miserably! He really struggles to read or write any template code beyond that which would be considered trivial. He also struggles with using some parts of the STL (eg vectors of vectors etc) which again we put down to his non-understanding of templates.

    Does anyone know of a simple guide to c++ templates - something along the lines of 'c++ templates for dummies' or 'The idiots guide to c++ templates'. This issue regarding templates and this person is getting serious as it's taking us longer to fix his template code as would be for us to write it ourselves in the first place. He knows and we all know he has this problem. He wants to be able to write non-trivial template code - and so do we!

    Does any one have any ideas please?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Learning templates

    Is it the syntax that is giving the trouble, or the reason for templates that isn't understood?

    Regards,

    Paul McKenzie

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Learning templates

    Mainly the syntax we think.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Learning templates

    Quote Originally Posted by 2kaud View Post
    Mainly the syntax we think.
    I think it all comes down to trial and error at first, and then that turns into experience when it comes to template syntax. Could it be the errors that are returned by the compiler are not easily deciphered, thus not diagnosed by your colleague without your help?

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Jul 2002
    Posts
    2,543

    Re: Learning templates

    My way of learing templates was somewhat controversial. I had problems writing and even using templates, and one day started to read "Modern C++ Design" (Alecsandrescu). After reading 5% of the book and understanding 1% of this, I found myself able to write template classes and to use STL. I am not sure this way is applicable to everyone...

  6. #6
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Learning templates

    There is a chance that he is simply incapable of thinking in the generic way that is required to properly design (and to a degree even use) templates.

    There's actually quite a leap between the analysing a particular specific problem and making an algorithm to solve it, and the abstract concept of a generic solution and tailoring a solution that will handle the application of specific types to that solution.

    The "syntax" problem is one you can learn to overcome. But the conceptual one is next to impossible to solve. Some peoplpe just don't have the mindset to it. It may come as "natural" to you to think in templates but that doesn't make it so for everyone.

    It isn't essentially any different to how some people can't bend their head around writing anything other than the most trivial programs. Or how some proficient programmers at say C/C++ struggle with an abstract programming language like prolog/lisp.

    You may have to live with the fact that "you cannot solve this".


    if it's a syntax issue. Solve it with starting with a simple task (without templates). THen asking to do the same for another type. And another type... Then showing how templates can do the "and for another type" in their place. And how you go from the specific example to the template one.
    (some people will Always need to do it that way, and you won't be able to learn them how to think in a template solution from the start).

    Also... typedefs.... Lots of typedefs. doing it properly makes things a lot easier by hiding a lot of the "dirty" syntax.

  7. #7
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Learning templates

    Thanks to all who replied with useful suggestions.

    I've spent a lot of 1-1 time with him this weekend and the problem is with the syntax. He can talk quite intelligently about various aspects of templates - he just has trouble writing the correct code. He says there's far too many :: and < and > !! Using OReubens suggestion re using lots of typedefs has helped a lot by making the syntax 'cleaner'.

    He's also been studying some internet c++ tutorials (http://www.codeproject.com/Articles/...mplates-Part-1 & http://www.codeproject.com/Articles/...mplates-Part-2) which have also helped by explaining things simply from the beginning.

    He has raised one point regarding typedefs. For function templates, he would like to define a typedef before the function header. ie he wants to be able to code

    Code:
    template<typename T>
    typedef std::vector<T> tvec;
    tvec myfunc(tvec v1, tvec v2)
    {...}
    As it is, the standard doesn't allow this. If any guru reading this post has any influence with c++ standards, can this be considered to be incorrporated into a future c++ standard?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  8. #8
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Learning templates

    what you're asking is "sort of" a "typedef template"
    this was in the proposals for C++0x (the C++11 we now have) but didn't make it through.

    but it's still a different thing than what you're doing here.

    the problem here is that the template<> ends at the ';'
    and since this isn't currently supported you get a error.

    the tvec myfunc(...) is a separate thing entirely, and assuming the above was typedef template was legal, would be invalid here, because you didn't start with a template<> definition.

    it's unlikely the above syntax would ever get adopted, it'd be a nightmare on the parsers.


    you can "sort of" solve this either with a macro instead of a typedef (with a bunch of issues of it's own, introducing more global names being the big issue)
    Code:
    #define  tvec    std::vector<T>
    
    template <typename T>
    tvec myfunc(tvec v1, tvec v2)
    {..}
    or by making the function a static member function of a class template (which will introduce one of those :: at the caller side).
    Code:
    template <typename T>
    class foo
    {
    public:
    	typedef std::vector<T> tvec;
    
    	static tvec myfunc(tvec v1, tvec v2)
    	{...}
    };
    
    /* call with (for vector of int):
      a = foo<int>::myfunc(b,c);
    */
    of course, you CAN typedef concrete types and use those types in a function definition.
    Code:
    typedef std::vector<int> intvec;
    
    intvec myfunc(intvec v1, intvec v2)
    {...}

  9. #9
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Learning templates

    Thanks for the useful suggestions. I try to avoid and discourage using macros for the reason you stated, but in this case might stretch a point using suitable names. I'll discuss further with him. But he's now making good progress with templates.

    it's unlikely the above syntax would ever get adopted, it'd be a nightmare on the parsers.
    I thought compiler writers enjoyed a challange!
    Last edited by 2kaud; September 2nd, 2013 at 08:03 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  10. #10
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Learning templates

    P.S. note that C++11 does allow for a new typedef syntax or rather "template alias" that may come close to what you expect, although it still needs twiddling with the <> on the template function as well....

    Code:
    template <typename T>
    using myvec = std::vector<T>;
    
    template <typename T>
    tvec<T> myfunc(tvec<T> v1, tvec<T> v2)
    {...}
    this syntax isn't supported in VC2010, I don't know if it has been implemented in VS2012 or the VS2013 beta yet (I'm not yet using those myself).

  11. #11
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Learning templates

    Quote Originally Posted by OReubens View Post
    P.S. note that C++11 does allow for a new typedef syntax or rather "template alias" that may come close to what you expect, although it still needs twiddling with the <> on the template function as well....

    Code:
    template <typename T>
    using myvec = std::vector<T>;
    
    template <typename T>
    tvec<T> myfunc(tvec<T> v1, tvec<T> v2)
    {...}
    this syntax isn't supported in VC2010, I don't know if it has been implemented in VS2012 or the VS2013 beta yet (I'm not yet using those myself).
    Unfortunately this has not been implemented in either VS2010 or VS2012. It also looks like it's not currently in VS2013 either
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Tags for this Thread

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