1

Given the code:

template <typename...> struct list{};

template <typename> list<> foo{};

template <typename H, typename... Ts>
list<H, decltype(foo<list<Ts...>>)> foo<list<H, Ts...>>{};

what is the type of foo<list<int, double>>?

I'm asking this because MSVC (v19.37) behaves here differently from all the other compilers I've tested this with (namely, gcc 13, clang 16, and icx 2023.2). While the other compilers use the recursive specialization in decltype (and so the type for them is list<int, list<double, list<>>>), MSVC doesn't (thus it gives list<int, list<>>).

The questions obviously are:

  1. Is this code correct (let's say, according to the C++20 standard, for certainty) and the type in question well-defined?
  2. If so, which compiler is right?
Jarod42
  • 203,559
  • 14
  • 181
  • 302
rm -rf
  • 121
  • 4
  • Is there a function `main` that goes with this? How are you determining the type a given compiler uses? – tbxfreeware Aug 27 '23 at 12:48
  • @tbxfreeware, yes there is, in the godbolt example (click the link above). I simply use std::is_same there, but any other way would work too. – rm -rf Aug 27 '23 at 12:56
  • Sorry. I did not check the link. I copied the GodBolt code, and ran it with MSVC, getting the same answer you did (i.e., 1). Unfortunately, however, your question is above my pay grade. I have no explanation for you. – tbxfreeware Aug 27 '23 at 13:10
  • Works with recursion on struct [Demo](https://godbolt.org/z/Pb7vdxzv4). – Jarod42 Aug 27 '23 at 13:48
  • @Jarod42, of course it does, MSVC would've been in all sorts of troubles if it didn't :-) The question is about variable templates specifically. – rm -rf Aug 27 '23 at 13:50
  • Trying to narrow down the issue... I suspect MSVC bug. – Jarod42 Aug 27 '23 at 13:53
  • Do you really need `list>>` as opposed to `list`? – n. m. could be an AI Aug 27 '23 at 14:43

0 Answers0