Skip to content

clang fails to consider the most specialized template definition when it's partialy specialized with multiple template template parameters #37392

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
llvmbot opened this issue Jul 3, 2018 · 11 comments
Labels
bugzilla Issues migrated from bugzilla c++ clang:frontend Language frontend issues, e.g. anything involving "Sema"

Comments

@llvmbot
Copy link
Member

llvmbot commented Jul 3, 2018

Bugzilla Link 38044
Resolution FIXED
Resolved on Jul 03, 2018 15:57
Version trunk
OS Windows NT
Reporter LLVM Bugzilla Contributor
CC @DougGregor,@zygoloid

Extended Description

I was doing simple metaprogramming recently and wanted to code the "front" helper: get the first type of a type list.
So I experimented on wandbox this code :

#include <type_traits>

template <typename... Ts>
struct type_list {
    static constexpr std::size_t size = sizeof...(Ts);
};

template <typename List, typename = void>
struct front {};

template <template <typename, typename...> class List, typename T, typename... Ts>
struct front<List<T, Ts...>, std::enable_if_t<(List<T, Ts...>::size > 0)>> {
    using type = T;
};

template <typename List>
using front_t = typename front<List>::type;

There's nothing fancy (Please let me know if the code above is not legal C++). Usage is :

static_assert(std::is_same_v<int, front_t<type_list<int, double>>>);

The problem is when I'm specializing with a template template like this :

template <typename, typename...> class List

Then clang throws it away and doesn't find anything.
However, if I specialize only with :

template <typename...> class List

Then it's fine and clang doesn't throw it away (but it's not ok because I can't do what I want to do this way).

I think clang is wrong here and should consider this specialization.
It consistently fails since clang 4.0 haven't tested the version before) up until trunk.

FWIW, both gcc and msvc compile it just fine.
Here's a godbolt repro : https://godbolt.org/g/M5fgbj

Please let me know if what I'm writing is not legal C++.

@zygoloid
Copy link
Mannequin

zygoloid mannequin commented Jul 3, 2018

Please let me know if the code above is not legal C++

Well, it's legal, but it's relying on a language change from the C++17 timeframe. See http://clang.llvm.org/cxx_status.html#p0522 and wg21.link/p0522.

Prior to that language change, type_list is not a valid template template argument for the List template template parameter of the front partial specialization, because the argument's template parameters don't match those of the parameter.

However, P0522 has a hole in it that causes it to reject significant amounts of existing code and sadly has still not been fixed (nor has a fix even been agreed by the C++ core working group), so we don't enable it by default. (GCC has implemented what they think the fix will be; I'm not sure what MSVC has done.)

You can use -frelaxed-template-template-args to enable support for P0522.

@llvmbot
Copy link
Member Author

llvmbot commented Jul 3, 2018

Thank you for your quick reply and explanation.
It's all clear now what's happening. I'll try using the -frelaxed-template-template-args option for clang and see what happens.

Now that I know where the problem is, I can make code fix so that I have matching template template parameter in case the option break too much of my existing code.

@llvmbot llvmbot transferred this issue from llvm/llvm-bugzilla-archive Dec 10, 2021
@shafik
Copy link
Collaborator

shafik commented Feb 1, 2023

@zygoloid @hubert-reinterpretcast Is there a DR tracking this hole in P0522 is it dr 2398 perhaps?

@cor3ntin
Copy link
Contributor

@zygoloid I ran into that again today, do you know if a fix is being pursued by anyone? Should clang adopt gcc's behavior?

@zygoloid
Copy link
Collaborator

@cor3ntin I don't know if anyone is working on this. I recall that we were going to try enabling -frelaxed-template-template-args by default and see if anything important (eg, boost) still breaks, but I don't think that's happened yet. DR2398 both looks to be closely related and gives an idea of what GCC is doing here -- if we can work out what that is, following that behavior might be reasonable.

@shafik
Copy link
Collaborator

shafik commented Apr 25, 2023

@erichkeane @AaronBallman maybe worth bringing this up in the next Clang Language Working Group meeting

@erichkeane
Copy link
Collaborator

There WAS an effort to turn that on not long ago... was it @ChuanqiXu9 ? I don't remember what is holding it up.

@ChuanqiXu9
Copy link
Member

I don't remember I have looked into such issues.

@cor3ntin
Copy link
Contributor

It was https://reviews.llvm.org/D109496
Apparently, it just fizzled out?

@erichkeane
Copy link
Collaborator

I thought we had one more recently than that as well? But I don't recall who was doing it.

@EugeneZelenko EugeneZelenko added the clang:frontend Language frontend issues, e.g. anything involving "Sema" label Apr 28, 2023
@llvmbot
Copy link
Member Author

llvmbot commented Apr 28, 2023

@llvm/issue-subscribers-clang-frontend

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugzilla Issues migrated from bugzilla c++ clang:frontend Language frontend issues, e.g. anything involving "Sema"
Projects
None yet
Development

No branches or pull requests

7 participants