The code below compiles and works the way I want with clang and gcc, but gives an error with RTM for Visual Studio 2015. I think clang and gcc are correct to allow this, but I'm not sure. Should this code compile?
#include <iostream>
#include <type_traits>
#include <utility>
template <typename T, size_t N, typename IS = decltype(std::make_index_sequence<N>{})>
struct Vector {
T e_[N];
};
template <typename T, typename U, size_t N, size_t... Is>
constexpr auto operator+(const Vector<T, N, std::index_sequence<Is...>>& x,
const Vector<U, N>& y) {
using V = std::common_type_t<T, U>;
return Vector<V, N>{x.e_[Is] + y.e_[Is]...};
}
int main() {
const auto v0 = Vector<float, 4>{1, 2, 3, 4};
const auto v1 = Vector<float, 4>{5, 6, 7, 8};
const auto v2 = v0 + v1;
for (auto x : v2.e_) std::cout << x << ", ";
std::cout << std::endl;
}
Visual Studio compiles this ok if I change operator + to:
template <typename T, typename U, size_t N, size_t... Is>
constexpr auto operator+(const Vector<T, N, std::index_sequence<Is...>>& x,
const Vector<U, N, std::index_sequence<Is...>>& y);
But I do not think that it is necessary to put again std::index_sequence<Is...>
for y
.