Implied template parameters.

Discussion in 'C++' started by jason.cipriani@gmail.com, Jun 5, 2008.

  1. Guest

    In this code:

    ===

    template <int N> class Base { };

    template <int N> class Other {
    public:
    explicit Other (Base<N> &) { }
    };

    int main () {
    Base<3> ok;
    Other<3> fine(ok);
    }

    ===

    Maybe this is a dumb question, but why do I have to specify the
    template parameter to "Other<3> fine(ok)"? Can't it deduce that it
    should be a 3 on it's own (since a Base<3> is being passed to the
    constructor, and Base<3> has no casting operators that could confuse
    it, there's no other type it could be)? I want to just say "Other
    fine(ok);".

    Jason
    , Jun 5, 2008
    #1
    1. Advertising

  2. James Kanze Guest

    On Jun 5, 3:25 am, ""
    <> wrote:
    > In this code:


    > ===


    > template <int N> class Base { };


    > template <int N> class Other {
    > public:
    > explicit Other (Base<N> &) { }
    > };


    > int main () {
    > Base<3> ok;
    > Other<3> fine(ok);
    > }


    > ===


    > Maybe this is a dumb question, but why do I have to specify
    > the template parameter to "Other<3> fine(ok)"? Can't it deduce
    > that it should be a 3 on it's own (since a Base<3> is being
    > passed to the constructor, and Base<3> has no casting
    > operators that could confuse it, there's no other type it
    > could be)? I want to just say "Other fine(ok);".


    The simple answer is: because the standard says so. Template
    type deduction only works for function templates. Other is a
    class template. Basically, in this case, the compiler must
    first know the type of Other, in order to know where it should
    look for the constructors. It can't do it in the reverse order.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Jun 5, 2008
    #2
    1. Advertising

  3. Guest

    On Jun 5, 5:17 am, James Kanze <> wrote:
    > On Jun 5, 3:25 am, ""
    > <> wrote:
    > > In this code:
    > > ===
    > > template <int N> class Base { };
    > > template <int N> class Other {
    > > public:
    > > explicit Other (Base<N> &) { }
    > > };
    > > int main () {
    > > Base<3> ok;
    > > Other<3> fine(ok);
    > > }
    > > ===
    > > Maybe this is a dumb question, but why do I have to specify
    > > the template parameter to "Other<3> fine(ok)"? Can't it deduce
    > > that it should be a 3 on it's own (since a Base<3> is being
    > > passed to the constructor, and Base<3> has no casting
    > > operators that could confuse it, there's no other type it
    > > could be)? I want to just say "Other fine(ok);".

    >
    > The simple answer is: because the standard says so. Template
    > type deduction only works for function templates. Other is a
    > class template. Basically, in this case, the compiler must
    > first know the type of Other, in order to know where it should
    > look for the constructors. It can't do it in the reverse order.


    That makes sense.

    Thanks for clearing that up,
    Jason
    , Jun 8, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kent Paul Dolan
    Replies:
    22
    Views:
    1,115
    goergebush
    Dec 22, 2005
  2. Russell Warren
    Replies:
    5
    Views:
    456
    Russell Warren
    Jan 17, 2006
  3. Don Lancaster

    Implied Document form problem.

    Don Lancaster, May 3, 2007, in forum: Javascript
    Replies:
    2
    Views:
    153
    -Lost
    May 4, 2007
  4. Andrew Poulos

    implied eval

    Andrew Poulos, Feb 2, 2009, in forum: Javascript
    Replies:
    8
    Views:
    125
  5. Richard Maher

    Interface with implied Constructor

    Richard Maher, Jul 9, 2013, in forum: Java
    Replies:
    58
    Views:
    746
    Steven Simpson
    Jul 27, 2013
Loading...

Share This Page