RE: [sv-ec] Matching defaults

From: Arturo Salz <Arturo.Salz_at_.....>
Date: Tue Jul 22 2008 - 17:39:55 PDT
Hi Randy,

 

What I meant is that C++ using the context of the unit of compilation
(file) to determine the symbols used as default values. This is perhaps
a little known quirk of the C++ language, but is nonetheless different
from SV. The following example uses three files, a.h, a.c and b.c to
show the C++ behavior:

 

            >>>> a.h <<<<

#include <iostream>

class C

{  public:

   void foo( int b = one ) {  std::cout << b << std::endl; }

};

 

>>>> a.h <<<<

static int one = 1;

#include "a.h"

extern void F(C &p);

 

int main()

{

   C p;

   p.foo();

   F( p );

}

 

>>>> b.h <<<<

static int one = 2;

#include "a.h"

void F(C &p)

{

   p.foo();

}

 

 

If you compile and run the example, it will print:

1

2

Hence, each call to method foo of the same object uses the default
argument defined in the caller's compilation unit. This example may seem
awkward, but one can construct a similar yet more useful scenario using
macros (#define one 2). Once you're bitten by this, you won't forget.

 

SV effectively plugs this hole - it creates a different class in each
compilation unit, and for particular class, the default argument is
always taken from the declaration - we just need to be specific as to
which declaration: class (prototype) declaration or (out-of-body) method
definition.

 

            Arturo

 

From: Randy Misustin [mailto:ram@model.com] 
Sent: Friday, July 18, 2008 11:53 PM
To: Arturo Salz
Cc: Rich, Dave; Mirek Forczek; Bresticker, Shalom; Surya Pratik Saha;
sv-ec@eda.org
Subject: Re: [sv-ec] Matching defaults

 

Arturo Salz wrote: 

Also, you forgot to mention the biggest difference between C++ and SV
default arguments. In C++, default arguments are interpreted in the
context of the caller, whereas in SV they are interpreted in the context
of the declaration. SV 3.1 had the same semantics as C++, but was later
changed to use the declaration context to avoid having the same
expression interpreted differently in the caller's context.


Hi Arturo,

I'm not certain I'm understanding what you mean by "interpreted in the
context of the caller", but if you're talking about symbol visibility, I
believe  C++, like SV, uses the context of the declaration to determine
which symbols are visible. For example, in the following snippit:

class Foo {
public:
        static int i;
        void fn1(int val = i) { cout << val << endl; }  // GOOD
        void fn2(int val = j) { cout << val << endl; }  // ERROR
};
 
int main()
{
        int j = 23;
        Foo *f = new Foo();
 
        f->fn1();
        f->fn2();
}


fn1 is a legal declaration, whereas fn2 will generate an error message
due to 'j' being undeclared.

-randy.


-- 
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.
Received on Tue Jul 22 17:40:48 2008

This archive was generated by hypermail 2.1.8 : Tue Jul 22 2008 - 17:41:32 PDT