#include <iostream>
struct U
{
template<typename T>
operator T();
};
template<unsigned I>
struct X : X<I - 1> {};
template<>
struct X<0> {};
template<typename T>
constexpr auto f(X<4>) -> decltype(T(U{}, U{}, U{}, U{}), 0u) { return 4u; }
template<typename T>
constexpr auto f(X<3>) -> decltype(T(U{}, U{}, U{}), 0u) { return 3u; }
template<typename T>
constexpr auto f(X<2>) -> decltype(T(U{}, U{}), 0u) { return 2u; }
template<typename T>
constexpr auto f(X<1>) -> decltype(T(U{}), 0u) { return 1u; }
template<typename T>
constexpr auto f(X<0>) -> decltype(T{}, 0u) { return 0u; }
struct A
{
void* a;
int b;
double c;
};
int main() { std::cout << f<A>(X<4>{}) << std::endl; }
The code above is accepted by both of gcc and clang. However, gcc gives the expected output 3
; other than the unexpected output 1
given by clang.
See: https://godbolt.org/z/YKnxWah1a
Related Q&A: Why does Clang 12 refuse to initialize aggregates in the C++20 way?
Which is correct in this case?