A proposal for named arguments for C++
open-std.org
open-std.org
Stdcall convention in C also often does some name mangling (leading underscore, trailing @ and the number of bytes on stack) but not always.
Also a C++ ABI would need to extend beyond simple calling conventions and also specify fixed behaviour of vtables, which i think compiler authors might balk at.
Or am I misusing terminology?
The problem with C++ ABIs is that C++ APIs tend to include much more substantial details in their headers. If you want to call a template or inline function, then you need a C++ compiler. Fundamentally, though, this is not any different from C macros -- a macro-heavy C API is going to require a C compiler to use.
You may be thinking of the funny situation on Windows where GCC has historically flagrantly disregarded the platform C++ ABI and implemented its own instead. I assume this decision was made as a matter of practicality: The Cygwin people didn't have the resources to try to implement Microsoft's ABI, whereas porting over the existing code for the Linux C++ ABI was a lot easier. Also, MSVC tends to take much longer to implement new C++ language features than GCC does which means its ABI could often be missing things GCC needs.
FWIW, Clang's port to Windows is taking the correct approach: they're implementing Microsoft's ABI, and will thus be able to link against MSVC-built C++ library. http://clang.llvm.org/docs/MSVCCompatibility.html
For some evidence:
See an interesting reddit thread: http://www.reddit.com/r/cpp/comments/13zex3/can_vs2010_c_lib...
And a link to a msvc blog post which lists the sizes of various containers under various versions of MSVC: http://blogs.msdn.com/b/vcblog/archive/2011/09/12/10209291.a...
Note that vector<int> on x64 shrunk from 48 to 24 bytes between VC9 SP1 and VC11. That's not an ABI, it's source compatibility.
Microsoft consider MSVC's C++ ABI as being internal to the compiler. They don't guarantee that the ABI won't change between major versions, only that it won't change between minor updates. Either way, it is not considered to be part of Windows, unlike the C ABI.
It's also not completely documented, so you'd have to reverse-engineer most of it if you wanted to be compatible.
It's the platform's de-facto C++ ABI, certainly, and in practice it's somewhat stable, but hardly reliable or useful for anyone other than Microsoft.
Other compilers and platforms used to have the same problem - each compiler had it's own ABI, and on platforms where one compiler was dominant, it basically became the de-facto standard, but there was no interoperability. That changed when pretty much every compiler (aside from MSVC) and OS (aside from Windows) adopted the Itanium C++ ABI, or a derivitive (like the ARM C++ ABI).
The C++ standards organization have a draft proposal to have platform standard ABIs, specified and documented by the platform holder. This really only affects Windows, and would require Microsoft to define a C++ ABI for Windows. That would most likely mean documenting the MSVC ABI, and calling that the standard C++ ABI.
Various Microsoft employees are pushing for it, so it might happen. The proposal was written by Herb Sutter, for example.
I really hope it makes it into the language. A stable API for external functions and whatever ABI you want for internal functions.
http://www.swag.uwaterloo.ca/acd/docs/ItaniumC++ABI.htm
Almost every major vendor has been working towards this for a long time now.Microsoft of course remains "special".
Regardless, the C++ standards committee did look at creating a portable C++ ABI in May 2014:
https://isocpp.org/blog/2014/05/n4028Or features are wrong? What's exactly wrong with this feature? Its improvement.
void foo(static int name) { ... }
I don't think they're 'static' for anything on parameters yet.
rectangle->set(10,10,20,20,true,false)
you get something that can be understood without taking a trip to the API docs: [rectangle setX:10 y:10 width:20 height:20 updateBSP:YES clipToParent:NO]
It's a pity they are moving away from this syntax in Swift. It's the right move -- I hear nothing but hate for it from those who don't actually use it (i.e. the people who should be Swift's target audience) -- but it's still a pity.Otherwise, it would be very confusing that two different syntax are used for similar purpose.
func join(string s1: String, toString s2: String, withJoiner joiner: String)
-> String {
return s1 + joiner + s2
}
join(string: "hello", toString: "world", withJoiner: ", ")
// returns "hello, world"
[1]: https://developer.apple.com/library/ios/documentation/swift/...Here's a similar hack that works in C++11, though it unfortunately requires declaring a global variable for each name (which I make less-bad by prefixing them with $, which is non-standard but supported by all major compilers):
Both GCC and clang complain about such initializer "overriding", but I habitually disable those warnings whenever I start a new project. (I also contributed the patch to clang to be able to disable that warning =)
Initializer overrides and missing braces are the two dumbest diagnostics that, sadly, both GCC and clang spit out. Using '{ 0 }' is the _only_ way to initialize a compound automatic variable where you do not know (and shouldn't depend on) the internal layout, and it's perfectly legal C.
Not only that, but why are you passing so many parameters like top, bottom, left, right? At least put them in a struct if you can't make them fields of the object itself.
This feature seems like "too little, too late". Maybe 30 years about it would have been helpful.
As far as the IDE goes, for the sort of code I write, reading is more important than writing. When I read the code I'd rather see what it does than what I think it does.
foo(36,,,54)
sure it won't do if there are dozens but for the odd ugly case where you naughtily use optional parameters it's OK.
It sounds like there's a need for IDEs to show the parameter names all the time. They could just be inserted automatically into the display if you want to see them, or turn them off if you don't like that. Better than being always on like this proposal.
That being said, this could hurt readability in some cases as single lines of code are going to get larger, especially with length parameter names. Another issue I forsee is a code style where parameters are prefixed with p_. Having to type that prefix each time would be annoying, but finding a readable syntax that allows a separately specified name will also be confusing.
int myfunc(float x, y, z, r, g, b, a);
instead of int myfunc(float x, float y, float z, float r, float g, float b, float a);
Yes, I know about structs, but often that's not applicable. And if the type name is some long thing, then you have to really look carefully to notice they are all the same type. It seems this style is now being seen as a feature, although I don't know of any language that required repeating the type for each parameter until C and C++ adopted this for the new style function prototype declarations. I was very sorry to see that D has also adopted this "feature". I believe go allows multiple declarations of the same type. I'd much rather see this fixed in C and C++ than have them clutter up things with named parameters.This got me thinking of the old C ways: http://msdn.microsoft.com/en-us/library/efx873ys.aspx
The problem that they're trying to solve is that people often use 5 arguments when a single struct would do.
> In this proposal, the association of parameter names with a function for the purpose of making calls with named arguments to that function happens locally in each translation unit, and new declarations within a translation unit can change a function's ability to be called with named arguments at call sites below the new declaration (by using different names than a previous declaration).
However, I do find named parameters suspect, not just because I don’t care for names generally, but also because they’re just papering over the problem of a function with many parameters, some of which have the same type.
Faced with that problem, I usually reach for more descriptive types: enums instead of booleans, numbers that know about their units and axes and coordinate spaces, that sort of thing. Ideally, all arguments have different types and there is only one permutation of their order that the compiler will accept. It sounds onerous, but most of your functions probably satisfy this constraint already.
Whatdoesitdo (i++, i) func(5,0,0,NULL,0,true,true,false,NULL,true);
func(verbose: true);
With named parameters, I can just specify the differences between my function call and the default values.a Windows programmer by any chance? :)
void drawBox(int X,int Y,int W,int H);
void drawBox(int TL,int TR,int BL,int BR);
Having an extra safety for mature code offers an immediate protection, in a much more accessible fashion then competing ideas like 'concepts'.If I'm reading this correctly, it ends up basically boiling down to a preprocessor trick; you don't get the advantage of being able to add parameters to a function, and have name-based calls to it continue to do the right thing without a recompilation.
typedef int annotated_type;
void foo (annotated_type parameter_name) {}
then I could write this: foo (5); /* the short and alegedly ambiguous version of function calling */
or this: foo ((annotated_type) 5); /* now I know more about what "5" means
in this function parameter's context */
There could be a lot of annotated types around, and their presence would be optional, so the code could be cluttered for the sake of making things more explicit only where it would be really needed!Of course, this doesn't come with parameter-order juggling, but that is another can of worms in itself, which I'd better avoid!