How does the compiler interpret a template argument (reference vs. non-reference) ?

Discussion in 'C++' started by mrstephengross, Sep 7, 2005.

  1. I'd like to know if there's a standard way that the compiler interprets
    a template argument. That is, let's say you pass a Foo instance as a
    template argument to some function. Is the instance itself passed, or
    does the compiler assume it's a Foo reference? For example:

    ================================
    template<typename I> someFunc(I i) { /* ... */ }

    Foo f;

    someFunc(f);
    =================================

    In someFunc(), does I = Foo, or I = Foo&, or I = const Foo & ?

    Thanks,
    --Steve
     
    mrstephengross, Sep 7, 2005
    #1
    1. Advertising

  2. mrstephengross

    mlimber Guest

    mrstephengross wrote:
    > I'd like to know if there's a standard way that the compiler interprets
    > a template argument. That is, let's say you pass a Foo instance as a
    > template argument to some function. Is the instance itself passed, or
    > does the compiler assume it's a Foo reference? For example:
    >
    > ================================
    > template<typename I> someFunc(I i) { /* ... */ }
    >
    > Foo f;
    >
    > someFunc(f);
    > =================================
    >
    > In someFunc(), does I = Foo, or I = Foo&, or I = const Foo & ?


    The first. You can make it the second or third by adding those
    parameters to your template, e.g.,

    template<typename I> someFunc(I& i) { /* ... */ }

    Cheers! --M
     
    mlimber, Sep 7, 2005
    #2
    1. Advertising

  3. Can I also explicitly cast the Foo instance in the someFunc()
    invocation?

    For example: someFunc(const Foo &)f);

    --Steve
     
    mrstephengross, Sep 7, 2005
    #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. kuangye
    Replies:
    2
    Views:
    329
    Barry
    Apr 10, 2008
  2. nw
    Replies:
    0
    Views:
    332
  3. nguillot
    Replies:
    5
    Views:
    539
  4. avasilev
    Replies:
    4
    Views:
    585
    avasilev
    Dec 22, 2011
  5. Replies:
    3
    Views:
    168
    Victor Bazarov
    Apr 20, 2013
Loading...

Share This Page