Skip to content

[BUG] Support _ unnamed template parameter. #812

Closed

Description

@JohelEGP

Title: Support _ unnamed template parameter.

Description:

Commit 6ce2643 added support only for function parameters.

Minimal reproducer (https://cpp2.godbolt.org/z/WY56cMoTd):

f: <_, _> (_, _) = { }
main: () = { }
Commands:
cppfront main.cpp2
clang++18 -std=c++23 -stdlib=libc++ -lc++abi -pedantic-errors -Wall -Wextra -Wconversion -Werror=unused-result -I . main.cpp

Expected result:

The template parameters to be named similar to the function parameters.

template<typename tparam1, typename tparam2> auto f([[maybe_unused]] auto const& param1, [[maybe_unused]] auto const& param2) -> void;

Actual result and error:

template<typename _, typename _> auto f([[maybe_unused]] auto const& param1, [[maybe_unused]] auto const& param2) -> void;
Cpp2 lowered to Cpp1:
//=== Cpp2 type declarations ====================================================


#include "cpp2util.h"



//=== Cpp2 type definitions and function declarations ===========================

template<typename _, typename _> auto f([[maybe_unused]] auto const& param1, [[maybe_unused]] auto const& param2) -> void;
auto main() -> int;


//=== Cpp2 function definitions =================================================

template<typename _, typename _> auto f([[maybe_unused]] auto const& param1, [[maybe_unused]] auto const& param2) -> void{}
auto main() -> int{}
Output:
build/main.cpp:12:31: error: declaration of '_' shadows template parameter
   12 | template<typename _, typename _> auto f([[maybe_unused]] auto const& param1, [[maybe_unused]] auto const& param2) -> void;
      |                               ^
build/main.cpp:12:19: note: template parameter is declared here
   12 | template<typename _, typename _> auto f([[maybe_unused]] auto const& param1, [[maybe_unused]] auto const& param2) -> void;
      |                   ^
build/main.cpp:18:31: error: declaration of '_' shadows template parameter
   18 | template<typename _, typename _> auto f([[maybe_unused]] auto const& param1, [[maybe_unused]] auto const& param2) -> void{}
      |                               ^
build/main.cpp:18:19: note: template parameter is declared here
   18 | template<typename _, typename _> auto f([[maybe_unused]] auto const& param1, [[maybe_unused]] auto const& param2) -> void{}
      |                   ^
2 errors generated.

See also:

Activity

  1. gregmarr commented on Nov 9, 2023

    @gregmarr
    Contributor

    I'm curious what the use case is for unused template parameters. Is it for specializing an existing template?

  2. JohelEGP commented on Nov 9, 2023

    @JohelEGP
    ContributorAuthor

    Compilers don't seem to warn on these.
    But I have to invent names,
    and maybe I'll run out of T, U, V, W, ???.
    Of course, this mostly happens in tests.

  3. gregmarr commented on Nov 9, 2023

    @gregmarr
    Contributor

    Okay, thanks.

  4. hsutter commented on Nov 10, 2023

    @hsutter
    Owner

    Thanks!

  5. JohelEGP commented on Nov 24, 2023

    @JohelEGP
    ContributorAuthor

    This is now #877.

    Repeat of #877

    It doesn't work for NTTPs: https://cpp2.godbolt.org/z/rdb6dbvv1.
    Also, <_...: _> doesn't lower to a parameter pack declaration.

    t: @struct <_...: _> type = {
      f: <_: int> () -> i32 = 0;
    }
    main: () = { }
    
    template<auto _> class t {
      public: template<int _> [[nodiscard]] static auto f() -> cpp2::i32;
    };
    main.cpp2:2:24: error: declaration of '_' shadows template parameter
        2 |   public: template<int _> [[nodiscard]] static auto f() -> cpp2::i32;
          |                        ^
    main.cpp2:1:15: note: template parameter is declared here
        1 | template<auto _> class t {
          |               ^
    
  6. added a commit that references this issue on Dec 5, 2023
    8861b51
  7. added a commit that references this issue on Feb 1, 2024
    76c2941
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

    bugSomething isn't working

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions