473,789 Members | 2,388 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Q about default function parameters

Why can I do this:

template<typena me A, typename B=A>
struct X { /*...*/ };

whereas this gives me an error about an undeclared identifier with
MSVC++ 7.1:

struct A
{ A(int arg1, int arg2=arg1); };

Now I know that the C++ standard says "you can't do that" (section
8.3.6 paragraph 9), but from a parsing standpoint, it seems like if
the compiler can handle the template parameter, it should have no
problem with the function parameter?

Thanks.

--
Bob Hairgrove
No**********@Ho me.com
Sep 29 '05 #1
1 1632
* Bob Hairgrove:
Why can I do this:

template<typena me A, typename B=A>
struct X { /*...*/ };

whereas this gives me an error about an undeclared identifier with
MSVC++ 7.1:

struct A
{ A(int arg1, int arg2=arg1); };

Now I know that the C++ standard says "you can't do that" (section
8.3.6 paragraph 9), but from a parsing standpoint, it seems like if
the compiler can handle the template parameter, it should have no
problem with the function parameter?


I think you should post this question to [comp.std.c++].

The paragraph you refer to says it's because the function argument evaluation
order is not defined (template parameters have no such problem).

I.e. if 'arg2' were evaluated first it would refer to an indeterminate or
non-existent value, the way the language is.

However, on the other hand, if it were allowed it could simply _restrict_ the
evaluation order, and with one exception I see no particular problem with
that. The exception: in C, for stack-based argument passing, arguments are
conventionally pushed (and hence, for efficiency, evaluated) right to left, in
order to support variable number of arguments, the C++ "..." notation. But as
I see it this is not a showstopper problem, because the non-"..." (first N)
arguments can be pushed and evaluated left to right, after pushing and
evaluating the "..." arguments right to left, and with this scheme the
function implementation can refer to the non-"..." arguments at fixed offsets
from the stack pointer, just as before.

On the third hand, you can obtain just about the same effect by writing

struct A
{
A( int a1 ): myX( a1 ), myY( a1 ) {}
A( int a2, int a2 ): myX( a1 ), myY( a2 ) {}
...
};

which does suffer a little from the lack of constructor forwarding, but if or
when we get constructor forwarding that will perhaps completely solve this.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Sep 29 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

46
3528
by: J.R. | last post by:
Hi folks, The python can only support passing value in function call (right?), I'm wondering how to effectively pass a large parameter, such as a large list or dictionary? It could achieved by pointer in C++, is there such way in Python? Thansk in advance. J.R.
12
12710
by: earl | last post by:
class temp { public: temp(); foo(char, char, char*); private: char matrix; }; temp::foo(char p, char o, char m = matrix )
3
3507
by: Schwarzbauer Günter | last post by:
Hello, Does the C++ standard allow default parameters when defining a typedef for a function type, e.g. typedef void (*TYPE_Report)(const char* message, const char* details = 0); This was accepted by MS Visual C++ 6.0, but is rejected in Visual Studio ..NET 2003. I assume that this change might be because Visual Studio .NET is more C++
2
23358
by: Vic Y | last post by:
Hi, I am trying to call a user defined function(UDF) from a stored proc, using a default parameter in the called UDF (e.g. @bid_price_type int = 0 ). However the calling stored proc complains that the UDF is expecting a 2nd parameter when I only provide the @test parameter value. Are default parameter values supported in UDF's? I wld really appreciate anybody's help. Thankyou in advance.
4
5850
by: aling | last post by:
What's the rule of default argument of function in C++? I found that the default argument of function could not necessary be a constant value. Is it true? Previously I thought that the default argument of function must be a constant value. Here is my sample code, it's compiled successully in VC7.1. #include<iostream> class Base{ public :
3
2040
by: Ed Morton | last post by:
Anyone know why "const" isn't the default for function parameters? I almost never see code where function parameters are modified intentionally (and when I do, I usually think copying the value to a local variable would be clearer), but I do occasionally see function parameters being incremented instead of similair-sounding local variables or, in the case of pointers, instead of the objects they point to. e.g.: void foo(int *x, int...
8
3046
by: cody | last post by:
Why doesn't C# allow default parameters for methods? An argument against I hear often is that the default parameters would have to be hardbaken into the assembly, but why? The Jit can take care of this, if the code is jitted the "push xyz" instructions of the actual default values can be inserted. To make things simpler and better readable I'd make all default parameters named parameters so that you can decide for yourself why one to...
5
7421
by: Imran Aziz | last post by:
Hello All, I am new to C# , how can I delare parameters to a function as optional, and also how are default values assigned ? consider the function public String myFunc(String thisisOptional, String OptWithDefaultValue) { }
12
7874
by: claudiu | last post by:
Hi, I'll go straight to the first question. Why does the code below compile? void f(int i = 0); int main() { (&f)();
7
7070
by: jamesclose | last post by:
My problem is this (apologies if this is a little long ... hang in there): I can define a function in VB.NET with optional parameters that wraps a SQL procedure: Sub Test(Optional ByVal Arg1 As Integer = 0, _ Optional ByVal Arg2 As Integer = 0, _ Optional ByVal Arg3 As Integer = 0) ' Call my SQL proc with the same signature
0
9663
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10193
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10136
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9979
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7525
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6761
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5415
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5548
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2906
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.