I have this piece of code right there:
template <int V>
struct Constant {
constexpr operator int() const noexcept { return V; }
};
template <class T, int N>
struct Array { };
auto function(auto s) -> Array<int, s + s> {
return {};
}
auto const a = function(Constant<3>{});
To my greatest sadness, only Clang seems to accept this code.
Which compiler is right, and why?
std::integral_constant
into the constant and then does the addition while gcc and MSVS just try to add theinteger_constant
which doesn't have anoperator +
so the template parameter is invalid. Not sure who is right here but I feel it is gcc and MSVS since conversions are not supposed to be done in template argument deduction. – Cides
decays to int at compile time or not. – Pachysandras + s
is indeed a constant expression. You can totally usestd::array<int, s + s>
in the body of the function, and all compiler accepts it: godbolt.org/z/jdMd9aoWb – Pachysandraintegral_constant
but uses thatintegral_constant
to get the array size: godbolt.org/z/d7Tarz9f5. gcc still wont compile unless you use the deduced size: godbolt.org/z/6jo78xGqh – Cidestd::array<int, s>
in the trailing return type too. I would actually expect clang to be right since all compiler accepts the expression inside the body of the function: godbolt.org/z/czs1MKs6s – Pachysandra