Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

[BUG] Add support for defining template specializations. #467

Open

Description

@leejy12

AFAIK, there's no way to define template specializations in cppfront. I found this out when trying to define a custom std::formatter.

Here's my expected syntax.

Foo: @struct type = {
    x: i32 = 0;
}

std::formatter<Foo>: <> @struct type = {
    parse: (this, inout ctx: std::format_parse_context) -> _ = {
        return ctx.end();
    }

    format: <FormatContext> (this, foo: Foo, inout ctx: FormatContext) -> _ = {
        return std::format_to(ctx.out(), "Foo({})", foo.x);
    }
}

As of now, cppfront doesn't attempt to compile the std::formatter<Foo> part.

https://godbolt.org/z/eKd8Y9aWz

Activity

  1. added
    bug
    on May 23, 2023
  2. leejy12 commented on May 25, 2023

    @leejy12
    Author

    @JohelEGP Unrelated, but how will this code be compiled?

    same1: <T, U> type == std::false_type;
    same1<T, T>: <T> type == std::true_type;
    

    Because

    template <typename T, typename U>
    using same1 = std::false_type;
    
    template <typename T>
    using same1<T, T> = std::true_type;
    

    is invalid Cpp1.

  3. MaxSagebaum commented on May 31, 2023

    @MaxSagebaum
    Contributor

    Just an idea: Directly specifying the specialized template arguments in the declaration name comes from the current cpp1 syntax. In cpp2 syntax this could be moved nearly anywhere else. So a few examples:

    same1: <T, U> type = {
      this: std::false_type = ();
    }
    same1: <T> type<T, T> = { // 1: Have the specialized arguments defined in the type.
      this: std::true_type = ();
    }
    
    same1: <T> specialize <T, T> type = { // 2: Have a new keyword 'specialize' which defines the specialization 
      this: std::true_type = ();
    }
    same1: <T>  type = { 
      specialize: same1 = <T, T> // 3: Have a new keyword 'specialize' which defines the specialization in the body.
      this: std::true_type = ();
    }
    same1: <T>  type = { 
      same1: type == <T, T> // 4: Use the class name for the specialization.
      this: std::true_type = ();
    }
    

    I think 1. would have problems with the specialization of function templates.
    2 keeps the specialization near the current location.
    3 would be more in line with the new definition of inheritance.
    4 might be the weakest one.

    My preference would be 2.

  4. MaxSagebaum commented on May 31, 2023

    @MaxSagebaum
    Contributor

    Actually, first comes the template-argument-list (as lowered to Cpp1) before the template-parameter-list. Which matches "declaration follows use".

    same1: <T, T> for <T> type = {
      this: std::true_type = ();
    }
    

    I think in this example the declaration follows use would be broken.
    First you declare the type, then the template-argument-list and afterwards the template-parameter-list. But the templates in the template-parameter-list would be used in the template-argument-list before they are declared.

    In

    same1: <T> specialize <T, T> type = { // 2: Have a new keyword 'specialize' which defines the specialization
      this: std::true_type = ();
    }
    

    we would follow the left to right principle:
    We declare a something with the name same1 it has the template-parameter-list <T> and is a specialization of the original template-parameters with the arguments <T, T> which is a type and has the definition ....

    For a function it would look like:

    f: <T> (a: T) -> T = { return 2 * a ; }
    
    f: specialize <std::string> (a: std::string) -> std::string = {return a + a;}
    
  5. Xeverous commented on Jun 3, 2025

    @Xeverous

    Thinking about the order - specialization parameters should be after primary ones - otherwise it won't be read left-to-right.

    I have came up with this:

    // primary
    is_same: @metafunction <T: type, U: type> type = {
        this: std::false_type = ();
    }
    // specialization
    is_same: @metafunction <T: type> is <T, T> type = {
        this: std::true_type = ();
    }
  6. MaxSagebaum commented on Jun 4, 2025

    @MaxSagebaum
    Contributor

    I also think this is the best option. See also #467 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions