Thread: Implicit template argument deduction for classes

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    88

    Implicit template argument deduction for classes

    Hi all,

    I just discovered that class template arguements are never implicitly deduced from their constructor arguements. Am I correct in thinking that the following code won't work (as I don't have access to a compiler)?

    Code:
    template<typename T> class basic_broken { /* ... */ };
    
    template<typename T>
    class broken { // bridge class
      basic_broken<T>* broken_impl;
    public:
      broken( T* t )
        : broken_impl(t)
      {}
    };
    
    void f()
    {
      int s;
      broken(&s); // Error: can't deduce template type of class broken?
    }

  2. #2
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    Yeah, that looks broken to me.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    88
    I feared so. Thanks Noir.

  4. #4
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    Why fear? It's not that big of an issue, and if you need that kind of thing, you can wrap the construction of an object in a factory function that's also a template and deduces its arguments:
    Code:
    template <class T>
    broken<T> make_more_broken( T arg ) {
      return broken<T>( arg );
    }

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Well, there's the forwarding issue. You have to write a function for each constructor. It's not insurmountable, but it sure is a hassle.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    If you use default arguments smartly and don't bog down the class with constructors, it's not even a hassle.

  7. #7
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Quote Originally Posted by Noir
    Why fear? It's not that big of an issue, and if you need that kind of thing, you can wrap the construction of an object in a factory function that's also a template and deduces its arguments:
    Code:
    template <class T>
    broken<T> make_more_broken( T arg ) {
      return broken<T>( arg );
    }
    Er, how would I call this function? The only ways that work seem to be

    Code:
    broken<int> code = make_more_broken(42);
    //or
    broken<int> code(make_more_broken(42));

  8. #8
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by anon
    Er, how would I call this function? The only ways that work seem to be

    Code:
    broken<int> code = make_more_broken(42);
    //or
    broken<int> code(make_more_broken(42));
    mostly these kinds of functions are used with other templates.

    e.g.
    Code:
    template <typename T>
    void print_thing(T t);
    
    print_thing(make_more_broken(42));
    in the next version of C++ the auto keyword will do what you want.
    Code:
    auto someVar = make_more_broken(42);
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  9. #9
    Registered User
    Join Date
    Oct 2005
    Posts
    88
    Quote Originally Posted by anon View Post
    Er, how would I call this function? The only ways that work seem to be

    Code:
    broken<int> code = make_more_broken(42);
    //or
    broken<int> code(make_more_broken(42));
    Isn't the point of the factory functions so you can do something like:
    Code:
    broken code = make_more_broken(42); // template arg of code automatically deduced
    Will that not work?

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    But you can't do that either. The point is that you can pass the result of the make_* function directly to a function template, e.g.

    Code:
    std::copy(in.begin(), in.end(), make_back_inserter(foo));
    Until the auto keyword in C++09, if you want a variable, you'll have to explicitly declare its full type.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Until the auto keyword in C++09, if you want a variable, you'll have to explicitly declare its full type.
    The auto keyword already exists; are they redefining it?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I believe they are just adding to its uses.

    http://www.open-std.org/jtc1/sc22/wg...2004/n1607.pdf
    Last edited by Daved; 03-30-2007 at 05:24 PM.

  13. #13
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Okay, thanks for the link.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  4. Nested loop frustration
    By caroundw5h in forum C Programming
    Replies: 14
    Last Post: 03-15-2004, 09:45 PM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM