www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 566] New: Adding non-static members and functions to classes doesn't error

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=566

           Summary: Adding non-static members and functions to classes
                    doesn't error
           Product: D
           Version: 0.174
          Platform: PC
               URL: http://www.digitalmars.com/d/template.html
        OS/Version: Windows
            Status: NEW
          Keywords: spec
          Severity: normal
          Priority: P4
         Component: DMD
        AssignedTo: bugzilla digitalmars.com
        ReportedBy: deewiant gmail.com
OtherBugsDependingO 511
             nThis:


Under the "Limitations" section, the spec states "[t]emplates cannot be used to
add non-static members or functions to classes" and showcases the following
code:

class Foo
{
    template TBar(T)
    {
        T xx;                   // Error
        int func(T) { ... }     // Error

        static T yy;                            // Ok
        static int func(T t, int y) { ... }     // Ok
    }
}

The lines marked with "// Error" don't fail to compile, they simply behave as
though they were declared static. The static attribute on yy and the second
func() is thus redundant.

Either the spec or DMD is wrong here.


-- 
Nov 18 2006
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=566


kamm incasoftware.de changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |kamm incasoftware.de





*** Bug 878 has been marked as a duplicate of this bug. ***


-- 
Jan 29 2007
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=566






Things seem to have changed a bit: now only the member variable is silently
made static while the member function works as a real non-static member
template.

class Foo
{
  template TBar(T)
  {
    T x;                   // Compiles, but is implicitly static
    void func(T t)   // Ok, non-static member template function
    { writefln(t); writefln(this.bar); }    
  }
  int bar = 42;
}

void main()
{
  Foo.TBar!(int).x = 2;
  Foo.TBar!(int).func(2); // error, since funcx is not static

  Foo f = new Foo;
  Foo g = new Foo;

  f.TBar!(int).func(2); // works

  f.TBar!(int).x = 10;
  g.TBar!(int).x = 20;
  writefln(f.TBar!(int).x); // prints 20
}


-- 
Jan 29 2007
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=566


kamm-removethis incasoftware.de changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |dvdfrdmn users.sf.net





-------
*** Bug 2015 has been marked as a duplicate of this bug. ***


-- 
Apr 19 2008
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=566






*** Bug 2015 has been marked as a duplicate of this bug. ***


-- 
Apr 19 2008
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=566






A slightly different test case.  The following compiles, but fails to link.

----
interface TestInterface 
  { void tpl(T)(); }
class TestImplementation : TestInterface 
  { void tpl(T)() { } }

void main()
{
  /* TestImplementation t = new TestImplementation(); // works */

  TestInterface t = new TestImplementation(); // fails

  t.tpl!(int)();
}
---


-- 
Apr 19 2008
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=566






I think the current behavior is useful, but it needs to be documented. The
second issue is an error that should be detected by the compiler.


-- 
Jun 28 2008
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=566


bugzilla digitalmars.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |FIXED





Fixed dmd 1.032 and 2.016


-- 
Jul 09 2008