"named parameters mechanism"

Discussion in 'C++' started by Adam Hartshorne, Jan 23, 2006.

  1. What is named parameter mechanism? Any ideas? I am looking through some
    code and there is a comment saying "VC++ has trouble with the named
    parameters mechanism", which i have no idea what this means.

    Any help much appreciated,

    Adam
    Adam Hartshorne, Jan 23, 2006
    #1
    1. Advertising

  2. Adam Hartshorne wrote:
    > What is named parameter mechanism? Any ideas? I am looking through some
    > code and there is a comment saying "VC++ has trouble with the named
    > parameters mechanism", which i have no idea what this means.
    >
    > Any help much appreciated,
    >
    > Adam


    http://www.parashift.com/c -faq-lite/ctors.html#faq-10.18

    Regards, Stephan
    =?iso-8859-1?q?Stephan_Br=F6nnimann?=, Jan 23, 2006
    #2
    1. Advertising

  3. Adam Hartshorne wrote:

    > What is named parameter mechanism? Any ideas? I am looking through some
    > code and there is a comment saying "VC++ has trouble with the named
    > parameters mechanism", which i have no idea what this means.
    >
    > Any help much appreciated,


    Also called "Named parameter idiom", see this newsgroup FAQ.
    (http://www.parashift.com/c -faq-lite/ctors.html#faq-10.18)

    HTH

    Fabio
    Fabio Fracassi, Jan 23, 2006
    #3
  4. Adam Hartshorne

    John Carson Guest

    "Adam Hartshorne" <> wrote in message
    news:43d4b4a9$
    > What is named parameter mechanism? Any ideas? I am looking through
    > some code and there is a comment saying "VC++ has trouble with the
    > named parameters mechanism", which i have no idea what this means.
    >
    > Any help much appreciated,
    >
    > Adam


    Consider a standard C++ function:

    void foo(int x, int * ptr)
    {
    //stuff
    }

    If you call the function with

    foo(a, b);

    then a is treated as an int, and b is treated as a int *, in accordance with
    the function declaration, i.e., the order in which arguments are supplied
    strictly determines the way in which they are interpreted. The first has to
    be an int and the second has to be an int*.

    With named parameters, by contrast, there is a syntax available whereby you
    can specify the type of the function arguments that you supply, thus giving
    freedom in the order in which arguments are entered.

    Since C++ does not support named parameters, except with elaborate
    workarounds, the comment either refers to such workarounds or to something
    completely different. I suspect it might be the latter (in which case the
    phrase is being misapplied).

    I suspect that the comment actually means is that VC++ 6 doesn't cope well
    with the following situation:

    #include <iostream>
    using namespace std;


    struct A
    {
    A()
    {cout << "This is A\n"; }
    };


    struct B
    {
    B()
    {cout << "This is B\n"; }
    };

    template <class T>
    void foo()
    {
    T t;
    }


    int main()
    {
    foo<A>();
    foo<B>();

    return 0;
    }

    Here you will note that the template parameter T is not used as a type in
    declaring foo's function parameters, i.e., we don't have, say,

    template <class T>
    void foo(T t)
    {}

    Accordingly, template parameters cannot be deduced from the function
    arguments supplied. This means you must distinguish different instantiations
    of the template by explicitly *naming* the template argument, as in foo<A>()
    and foo<B>().

    You will note if you run the code on VC++ 6 that it gets it wrong and the
    output is

    "This is B";

    both times.

    The workaround is to always use template parameters in the list of function
    parameter types, supplying dummy arguments as necessary when calling the
    function, e.g.,

    template <class T>
    void foo(T * ptr)
    {
    T t;
    }

    int main()
    {
    foo((A*)0);
    foo((B*)0);

    return 0;
    }



    --
    John Carson
    John Carson, Jan 23, 2006
    #4
    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.

Share This Page