Template argument deduction question

Discussion in 'C++' started by Dilip, Nov 15, 2006.

  1. Dilip

    Dilip Guest

    This is just an academic question but is there any particular reason
    why this does not work?

    template<typename T>
    class Foo
    {
    public:
    Foo(T x) : myvar_(x) { }
    private:
    T myvar_;
    };

    int main()
    {
    Foo xx(2); // why cannot the compiler deduce T as int?
    return 0;
    }
     
    Dilip, Nov 15, 2006
    #1
    1. Advertising

  2. Dilip wrote:
    > This is just an academic question but is there any particular reason
    > why this does not work?
    >
    > template<typename T>
    > class Foo
    > {
    > public:
    > Foo(T x) : myvar_(x) { }
    > private:
    > T myvar_;
    > };
    >
    > int main()
    > {
    > Foo xx(2); // why cannot the compiler deduce T as int?
    > return 0;
    > }


    Well, if I define a specialisation of your Foo template like this:

    template<> class Foo<void*>
    {
    public:
    Foo(int blah) {}
    };

    how should the compiler decide which Foo to use, Foo<int> or my
    Foo<void*>? So, the limitation is imposed that *you* have to tell
    the compiler what Foo to instantiate:

    Foo<int> whatever...

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Nov 15, 2006
    #2
    1. Advertising

  3. Dilip

    Guest

    Dilip wrote:
    > This is just an academic question but is there any particular reason
    > why this does not work?
    >
    > template<typename T>
    > class Foo
    > {
    > public:
    > Foo(T x) : myvar_(x) { }
    > private:
    > T myvar_;
    > };
    >
    > int main()
    > {
    > Foo xx(2); // why cannot the compiler deduce T as int?
    > return 0;
    > }


    from what i understand, template argument deduction doesnt apply to
    class templates.
    It only kicks in for functiion templates.

    I think I recall reading this from Vandevoorde's book, but anyone else
    who knows for sure can confirm this.
     
    , Nov 15, 2006
    #3
  4. Dilip

    Dilip Guest

    wrote:
    > from what i understand, template argument deduction doesnt apply to
    > class templates.
    > It only kicks in for functiion templates.
    >
    > I think I recall reading this from Vandevoorde's book, but anyone else
    > who knows for sure can confirm this.


    I know -- that's what I am reading too and that's where I picked up
    that example but I wanted to understand *why* that restriction was
    imposed. Victor, as usual, came to the resue.

    thanks!
     
    Dilip, Nov 15, 2006
    #4
  5. Dilip

    Guest

    Dilip wrote:
    > wrote:
    > > from what i understand, template argument deduction doesnt apply to
    > > class templates.
    > > It only kicks in for functiion templates.
    > >
    > > I think I recall reading this from Vandevoorde's book, but anyone else
    > > who knows for sure can confirm this.

    >
    > I know -- that's what I am reading too and that's where I picked up
    > that example but I wanted to understand *why* that restriction was
    > imposed. Victor, as usual, came to the resue.
    >
    > thanks!


    Unless I mis-understood Victor's messsage, but whatever he has said
    should apply to function templates too. You can do full specialization
    for function templates and still use parameter deduction where function
    overloading and other things will come into picture..
    But that's not the case for class templates.
     
    , Nov 15, 2006
    #5
  6. wrote:
    > Dilip wrote:
    >> wrote:
    >>> from what i understand, template argument deduction doesnt apply to
    >>> class templates.
    >>> It only kicks in for functiion templates.
    >>>
    >>> I think I recall reading this from Vandevoorde's book, but anyone
    >>> else who knows for sure can confirm this.

    >>
    >> I know -- that's what I am reading too and that's where I picked up
    >> that example but I wanted to understand *why* that restriction was
    >> imposed. Victor, as usual, came to the resue.
    >>
    >> thanks!

    >
    > Unless I mis-understood Victor's messsage, but whatever he has said
    > should apply to function templates too. You can do full specialization
    > for function templates and still use parameter deduction where
    > function overloading and other things will come into picture..
    > But that's not the case for class templates.


    With the functions they are simply added to the list for overload
    resolution. This mechanism already exists and is easy to use. With
    types (used for object creation) there is no such mechanism. The
    name has to represent a concrete (and complete) type. Why? There
    has to be an inherent difficulty for compiling tempalate, causing
    it to instantiate, figuring out its size, which constructor to call,
    etc. It must be much simpler with functions, that's why it exists
    there and not with types.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Nov 15, 2006
    #6
  7. Dilip

    Salt_Peter Guest

    Dilip wrote:
    > This is just an academic question but is there any particular reason
    > why this does not work?
    >
    > template<typename T>
    > class Foo
    > {
    > public:
    > Foo(T x) : myvar_(x) { }
    > private:
    > T myvar_;
    > };
    >
    > int main()
    > {
    > Foo xx(2); // why cannot the compiler deduce T as int?
    > return 0;
    > }


    Because the "int" belongs to Foo, not T - which is just a placeholder.
    The compiler needs to generate a new version of Foo< > for every type T
    needed. The compiler is not allowed to guess which one of those
    versions needs to be used either. What you can do, unlike functions, is
    set defaults.

    #include <iostream>

    template< typename T = int >
    class Foo
    {
    T t;
    public:
    Foo(const T& x = T()) : t(x) { }
    const T& get() const { return t; }
    };

    int main()
    {
    Foo< > foo(99);
    std::cout << "foo = " << foo.get() << std::endl;
    return 0;
    }
     
    Salt_Peter, Nov 15, 2006
    #7
  8. Dilip wrote:

    > This is just an academic question but is there any particular reason
    > why this does not work?
    >
    > template<typename T>
    > class Foo
    > {
    > public:
    > Foo(T x) : myvar_(x) { }
    > private:
    > T myvar_;
    > };
    >
    > int main()
    > {
    > Foo xx(2); // why cannot the compiler deduce T as int?
    > return 0;
    > }
    >


    Because here you really have not one, but two fairly independent templates: a
    class template and a member function (constructor) template. '2' is an argument
    for the latter, while 'T' is a parameter of the former.

    For some purposes you can "link" the two by introducing a "factory" function

    template<typename T> Foo<T> make_foo(T x) {
    return Foo<T>(x);
    }

    ...
    make_foo(2); // returns a temporary of type 'Foo<int>' initialized with '2'

    (a technique used, for example, in STL) but, of course, it is only usable for
    generating temporary objects and doesn't help with declarations.

    --
    Best regards,
    Andrey Tarasevich
     
    Andrey Tarasevich, Nov 16, 2006
    #8
    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. Replies:
    3
    Views:
    4,353
  2. Peng Yu

    Template argument deduction

    Peng Yu, Apr 16, 2005, in forum: C++
    Replies:
    1
    Views:
    510
    Mike Wahler
    Apr 16, 2005
  3. Bart Samwel
    Replies:
    14
    Views:
    815
    Bart Samwel
    Apr 22, 2005
  4. George
    Replies:
    4
    Views:
    414
    George
    Jan 13, 2006
  5. ma740988

    template argument deduction

    ma740988, May 29, 2006, in forum: C++
    Replies:
    5
    Views:
    1,459
    Bo Persson
    May 29, 2006
Loading...

Share This Page