Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > "named parameters mechanism"

Reply
Thread Tools

"named parameters mechanism"

 
 
Adam Hartshorne
Guest
Posts: n/a
 
      01-23-2006
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
 
Reply With Quote
 
 
 
 
=?iso-8859-1?q?Stephan_Br=F6nnimann?=
Guest
Posts: n/a
 
      01-23-2006
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-lit...html#faq-10.18

Regards, Stephan

 
Reply With Quote
 
 
 
 
Fabio Fracassi
Guest
Posts: n/a
 
      01-23-2006
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-lit...html#faq-10.18)

HTH

Fabio


 
Reply With Quote
 
John Carson
Guest
Posts: n/a
 
      01-23-2006
"Adam Hartshorne" <(E-Mail Removed)> wrote in message
news:43d4b4a9$(E-Mail Removed)
> 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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
skipping template parameters inside template parameters kito C++ 2 09-26-2010 12:04 AM
Class Member Data and Member Function Parameters - Should Parameters Be Data Members? Jason C++ 2 05-13-2006 07:11 AM
commands to configure WLAN adapter parameters =?Utf-8?B?SmFnYW4=?= Wireless Networking 0 10-05-2005 11:37 AM
does a "parameters"-parameter overwrite the "parameters"-object? Florian Loitsch Javascript 11 03-15-2005 03:33 PM
Servlet parameters different from the command line parameters? Jonck van der Kogel Java 2 05-26-2004 11:34 PM



Advertisments