Path: utzoo!attcan!uunet!cs.utexas.edu!rutgers!mcnc!rti!xyzzy!bigbird.rtp.dg.com!langley From: langley@bigbird.rtp.dg.com (Mark L Langley) Newsgroups: comp.lang.c++ Subject: default arg handling Message-ID: <1116@xyzzy.UUCP> Date: 10 Mar 90 15:04:44 GMT Sender: usenet@xyzzy.UUCP Reply-To: langley@bigbird.rtp.dg.com (Mark L Langley) Organization: Data General Corporation, Research Triangle Park, NC Lines: 86 Jonathon Shapiro (shap@delrey.sgi.com) bristles > >>langley@bigbird.rtp.dg.com (Mark L Langley) suggests... >> >> void bar(int x=i+j) { >> printf("%d\n", x); >> } >> >> main() { >> int i=20, j=30; >> bar(); >> } >> ...I intend to propose this as a change in ANSI C++, and have already >> proposed it to AT&T in private correspondence... > >This is a great example of why language design should be left to >people who know what they're doing. I'll just let that part ride. The problem however is that the dangerous aspects of this are legal now, depending on how declarations and definitions are broken across files. > >C already has a facility for doing this. It is called a macro. One problem in general with macros is that they aren't C and hence obey their own, more casual, scoping rules. But could you say a little more about how using macros helps default argument handling? Isn't one win of default arguments to avoid using a macro? > >The problem with what you suggest is that the function definition site >needs to know lots and lots of things about ALL function use sites. >It also would be altogether impossible to implement this across files >today. > >Functions should be isolated - they should not care who calls them, >except to the extent that they specify requirements in the form of >arguments. The above proposal violates this any number of ways. > How does it violate modularity? And I don't understand what you are getting at with ``sites.'' Now, as I clarified (I hope) in a later posting that you may not have seen, I am just suggesting that the declaration of a default argument to a function call, like the declaration of any other scoped object, be shadowed by declarations at a more local scope. My suggestion would not involve any more work than is done now. For example consider what happens now if you have two declarations of a function, in different parts of a system that have mutually exclusive declarations, but call the same function at run time. I assume of course that you realize that an application (now) can be broken across .c and .h files such that an intended default can be canceled out by an errant declaration. // foo.c - definition of foo extern "C" { extern printf(...); }; void foo(int x=1024) { printf("%d\n", x); } // main.c - usage of foo void foo(int x=100); main() { foo(); } Namely, since the actual default argument is supplied staticaly at call time the language processor detects no conflict. I just think redeclaring the default argument is legitimate; and should not be canceled by the occurrence of two declarations of foo being processed in the same compilation. Admittedly the above is the down side of what I am proposing. But things are no safer now, and there are useful aspects of redefining a default on purpose. >Try proposing it to the BASIC committee. They love this sort of >stuff. > >Jonathan Shapiro > Mark Langley langley@dg-rtp.dg.com