question on typedef and function template

Discussion in 'C++' started by subramanian100in@yahoo.com, India, Jan 3, 2008.

  1. , India

    , India Guest

    consider the following program:

    #include <iostream>
    #include <cstdlib>

    using namespace std;

    template<typename T> void fn(T const & arg)
    {
    cout << arg << endl;
    return;
    }

    int main()
    {
    const int x = 100;

    const int* const y = &x;

    fn(y);

    typedef const int* const Type;
    Type const & ref = y;

    cout << "from main(): " << ref << endl;

    return EXIT_SUCCESS;
    }

    Question 1:
    From the function template call 'fn(y)', the type of the template
    parameter deduced is 'const int* const'. However, the function
    template definition already has a const - that is, void fn(T const&
    arg). In this, T will get substituted by 'const int* const'. Am I
    correct ? If so, then won't we end up with void fn(const int* const
    const & arg) ? ie won't the instance of fn() have a duplicate const ?

    Question 2:
    consider
    typedef const int* const Type;
    Type const & ref = y;

    Here also, Type is defined to be 'const int* const' and so in the
    second line since we have Type const & ref, won't we end up with
    duplicate const - that is
    const int* const const & ref = y ?

    Kindly clarify.

    Thanks
    V.Subramanian
     
    , India, Jan 3, 2008
    #1
    1. Advertising

  2. , India

    Salt_Peter Guest

    On Jan 3, 1:44 am, ", India"
    <> wrote:
    > consider the following program:
    >
    > #include <iostream>
    > #include <cstdlib>
    >
    > using namespace std;
    >
    > template<typename T> void fn(T const & arg)
    > {
    > cout << arg << endl;
    > return;
    >
    > }
    >
    > int main()
    > {
    > const int x = 100;
    >
    > const int* const y = &x;
    >
    > fn(y);
    >
    > typedef const int* const Type;
    > Type const & ref = y;
    >
    > cout << "from main(): " << ref << endl;
    >
    > return EXIT_SUCCESS;
    >
    > }
    >
    > Question 1:
    > From the function template call 'fn(y)', the type of the template
    > parameter deduced is 'const int* const'. However, the function
    > template definition already has a const - that is, void fn(T const&
    > arg). In this, T will get substituted by 'const int* const'. Am I
    > correct ? If so, then won't we end up with void fn(const int* const
    > const & arg) ? ie won't the instance of fn() have a duplicate const ?
    >
    > Question 2:
    > consider
    > typedef const int* const Type;
    > Type const & ref = y;
    >
    > Here also, Type is defined to be 'const int* const' and so in the
    > second line since we have Type const & ref, won't we end up with
    > duplicate const - that is
    > const int* const const & ref = y ?


    no sir, the cv-qualifier introduced in the template type arguement is
    ignored in cases like these.
    the standard [8.3.2] 'References' says so specifically.

    If you still have doubts, change the function to:
    void fn(const int& arg) { ... }

    and you'll get something like:
    In function 'int main()':
    error: invalid initialization of reference of type 'const int&' from
    expression of type 'const int* const'
    error: in passing argument 1 of 'void fn(const int&)


    >
    > Kindly clarify.
    >
    > Thanks
    > V.Subramanian
     
    Salt_Peter, Jan 3, 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. Arkadiy Vertleyb
    Replies:
    5
    Views:
    2,340
    Arkadiy Vertleyb
    Sep 27, 2003
  2. Andrey Balaguta

    typedef for template function

    Andrey Balaguta, Nov 23, 2003, in forum: C++
    Replies:
    3
    Views:
    7,497
    Rob Williscroft
    Nov 23, 2003
  3. Adam Nielsen
    Replies:
    3
    Views:
    416
  4. oor
    Replies:
    0
    Views:
    1,374
  5. Replies:
    1
    Views:
    615
Loading...

Share This Page