C++ function parameter syntax

From all the programming language I've used or syntax I've learnt. I keep thinking that why is the parameter order is so hard to be remembered.

some code like these
 
string url = getRealLocation( dpart, 0, cache_dir, true );


Reading these kind of code require me to read the documentation on the code I or someone else wrote before.

Sure I can turn it to something more like

1
2
3
4
5
vector<string>& directoryPartition = dpart;
const int startIndex = 0;
const bool keep_cache = true;

string url = getRealLocation( directoryPartition, startIndex, cache_dir, keep_cache );


to make it clearer but it seems to be a hassle to do this for every function call there is and there might be a lot of conflicting variable name for the same parameter name but different function call. regarding performance I think the code above won't effect any because I trust that compiler is smart enough.

I've take a look at Javascript and this is the only language I've known that has code that can do this even though it isn't vanila code rather it use some library to do this easily

1
2
3
4
5
var spaceship = new Spacecraft({
   x : 0,
   y : 0,
   health : 1000
});



suddenly the order of the parameter doesn't matter and the it's quite clear what the magic number means...

Is there any big reason why this hasn't been implemented in other language ?
or is it just me who is out of date ?
Is there any big reason why this hasn't been implemented in other language ?
Nobody cares.
Actually there is proposal for that which might be implemented in C++1z
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4172.htm

In the meantime use named parameter idiom: http://www.parashift.com/c++-faq-lite/named-parameter-idiom.html
Or Boost::parameters http://www.boost.org/doc/libs/1_37_0/libs/parameter/doc/html/index.html#named-function-parameters
Speaking of other languages, C had it for 15 years:

1
2
3
4
5
int main(void)
{
    typedef struct { int health, x, y;} Spacecraft;
    Spacecraft spaceship = {.x=0, .y=0, .health=1000};
}

that wasn't introduced in any tutorial, book, or articles I've read.
I haven't even read a code that was written like that.

I should read more ...

but it doesn't seem to work in C++ compiler

1
2
3
4
5
6
7
8
9
10
11
12
struct CTest {
    int x;
    int y;
};

int main(){
    CTest t1 = { .y = 3, .x = 5}; // this doesn't works
    CTest t2 = { .x = 3, .y = 5}; // this works
    CTest t3 = { 3, 5 }; // this also works
    std::cout << t1.y << std::endl;
    return 0;
}


Is there any big reason why this hasn't been implemented in other language ?


I believe it was available in Ada88, or at least Ada9x, back when I was playing with that language. But this is a C++ forum.

(BTW, this is a feature I would be in favor of having, or at least investigating, for C++.)
Topic archived. No new replies allowed.