D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 13732 - Regular templates can use "template this", and they allow any type to be passed
Summary: Regular templates can use "template this", and they allow any type to be passed
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P1 normal
Assignee: No Owner
URL:
Keywords: pull
Depends on:
Blocks:
 
Reported: 2014-11-14 08:10 UTC by Meta
Modified: 2022-10-04 02:19 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Meta 2014-11-14 08:10:02 UTC
template Test(this ThisType)
{
    pragma(msg, ThisType);
}

void main()
{
    alias T = Test!int; // Prints "int"
}

This will also work for class templates, struct templates, alias templates, enum templates, etc., but not for templated methods. It doesn't matter if the template is a free template or nested within a struct or class.
Comment 1 Nick Treleaven 2022-09-07 07:09:56 UTC
What's the advantage over a normal template type parameter?
Comment 2 Meta 2022-09-09 10:48:06 UTC
import std.stdio;

class Base {
    void test1(T = typeof(this))() {
        writeln("test1: ", T.stringof);
    }
    
    void test2(this T)() {
        writeln("test2: ", T.stringof);
    }
}

class Derived: Base {
}

void main()
{
    new Base().test1();    //test1: Base
    new Base().test2();    //test2: Base
    
    new Derived().test1(); //test1: Base
    new Derived().test2(); //test1: Derived
}
Comment 3 Nick Treleaven 2022-09-09 15:11:00 UTC
Isn't comment 2 what we have already? The title says 'allow any type to be passed' and the example uses int.
Comment 4 Meta 2022-09-09 16:13:10 UTC
> Isn't comment 2 what we have already?

Ya, I created this issue because any template allows you to use the `this T` syntax, and it will accept any type (which seemed like a bug at the time). I still think it's confusing, and it does nothing in a template that's not nested in an aggregate, but might not necessarily be behaviour that needs to be fixed.
Comment 5 Nick Treleaven 2022-09-09 16:20:24 UTC
Oh I see, it should only be allowed for methods.
Comment 6 Dlang Bot 2022-09-14 13:32:28 UTC
@ntrel created dlang/dmd pull request #14434 "Fix Issue 13732 - non-member templates can use "template this"" fixing this issue:

- Fix Issue 13732 - non-member templates can use "template this"

https://github.com/dlang/dmd/pull/14434
Comment 7 Dlang Bot 2022-09-16 14:25:44 UTC
@ntrel created dlang/dmd pull request #14447 "Fix Issue 13732 - non-member templates can use "template this"" fixing this issue:

- Fix Issue 13732 - non-member templates can use "template this"
  
  Error if a TemplateThisParameter is declared where `typeof(this)` would
  be an error.
  Error if a TemplateThisParameter is passed a type that doesn't convert
  to `const typeof(this)`.
  
  Note: This is done with a dummy specialization, which causes the correct
  semantic error messages even for mixin and may be more efficient than
  checking this in dtemplate.d.

https://github.com/dlang/dmd/pull/14447
Comment 8 Dlang Bot 2022-10-04 02:19:28 UTC
dlang/dmd pull request #14434 "Fix Issue 13732 - non-member templates can use "template this"" was merged into master:

- c22f40bb47b98a0a18ffca0a6766047e81784d35 by Nick Treleaven:
  Fix Issue 13732 - non-member templates can use "template this"

https://github.com/dlang/dmd/pull/14434