reference type as template argument

Discussion in 'C++' started by subramanian100in@yahoo.com, India, May 14, 2008.

  1. , India

    , India Guest

    consider
    template<typename T> Test
    {
    // ...
    };

    We can have a pointer type as argument to a template class. For
    example, we can have,
    int x = 100;
    Test<int*> obj(&x); // assuming a suitable ctor exists

    But we cannot have reference type as template argument. This is
    because reference cannot be assigned (in the overloaded assignment
    operator) in the ordinary sense - that is, a reference cannot be
    reseated. So, we cannot instantiate a template class with reference
    type as argument. Is this understanding of mine, is correct ?

    Kindly clarify.

    Thanks
    V.Subramanian
    , India, May 14, 2008
    #1
    1. Advertising

  2. , India

    James Kanze Guest

    On May 14, 5:59 pm, ", India"
    <> wrote:
    > consider
    > template<typename T> Test
    > {
    > // ...
    > };


    > We can have a pointer type as argument to a template class. For
    > example, we can have,
    > int x = 100;
    > Test<int*> obj(&x); // assuming a suitable ctor exists


    > But we cannot have reference type as template argument.


    Sure you can.

    > This is because reference cannot be assigned (in the
    > overloaded assignment operator) in the ordinary sense - that
    > is, a reference cannot be reseated. So, we cannot instantiate
    > a template class with reference type as argument. Is this
    > understanding of mine, is correct ?


    No. Templates don't really pose any requirements on their
    arguments, provided it corresponds: the argument to a type
    parameter must be a type, the argument to an int parameter an
    int, etc.

    Actual templates typically will impose contraints, because they
    do something with the type they are given. Thus, the containers
    in the standard library require the type to be CopyConstructible
    and Assignable, because they need to be able to copy construct
    and assign objects of that type. You can't use references for
    them, because references don't meet the requirements. You can't
    use a class type which doesn't support copy construction or
    assignment, either.

    In practice, just about every real template will impose some
    constraints. And it is fairly difficult to write a template
    which will work equally well with both references and object
    types. (There are some in Boost. But if you look at the code
    for them, it's far from trivial.) But note that this is really
    no different from functions: it's rather frequent to have
    functions which take for example a double or an int, but
    restrict the range. Templates are really no different in this
    respect.

    --
    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, May 15, 2008
    #2
    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. mrstephengross
    Replies:
    2
    Views:
    388
    mrstephengross
    Sep 7, 2005
  2. nw
    Replies:
    0
    Views:
    294
  3. nguillot
    Replies:
    5
    Views:
    510
  4. Replies:
    3
    Views:
    158
    Victor Bazarov
    Apr 20, 2013
  5. Ryan Dupuis
    Replies:
    8
    Views:
    1,199
    Eric Sosman
    Oct 2, 2013
Loading...

Share This Page