I develop a cross-platform (Linux and Win) library. Within it I use of following helper templated functions to work with tuples (as example):
#include <iostream>
#include <string>
#include <tuple>
#include <utility>
#include <type_traits>
using namespace std;
template <class F, size_t... Is>
constexpr void index_apply_impl1(const F &f,
const index_sequence<Is...>&) {
int d[] = { 0, (f(integral_constant<size_t, Is> {}), 0)... };
}
template <size_t N, class F>
constexpr void index_apply1(const F& f) {
index_apply_impl1(f, make_index_sequence<N>{});
}
template <class Tuple, class F>
constexpr void apply1(const Tuple &t, const F &f) {
index_apply1<tuple_size<Tuple>{}>(
[&](auto &Is) { f(get<Is>(t)); } );
}
template <size_t N, class F>
constexpr void apply_by_index1(const F &f) {
index_apply1<N>(
[&](auto &&Is) { f(integral_constant<size_t, Is> {}); });
}
int main() {
auto t = make_tuple("aaa", 1, 11.11);
// does not work with gcc too
//apply1(t, [&](auto &v) { cout << v << endl;} );
apply_by_index1<tuple_size<decltype(t)>::value>([&](auto &&i) { cout << get<i>(t) << endl; });
}
This code is successfully compiled and works as expected with GCC 5.4 and 6.4, but it won't be compiled with MS Build tools 2015 (MS VC++ 2017 version is not checked yet). The error VC++ compiler prints is related to "auto"-type argument "Is" of the lambdas in the function "apply_by_index1". VC++ says:
zz.cpp(29): error C2975: '_Val': invalid template argument for 'std::integral_constant', expected compile-time constant expression
...
zz.cpp(38): error C2672: 'get': no matching overloaded function found zz.cpp(29): note: see reference to function template instantiation 'auto main::::operator ()>(std::integral_constant &&) const' being compiled
zz.cpp(38): error C2975: '_Idx': invalid template argument for 'std::get', expected compile-time constant expression
... and so on
Is it generaly possible to implement such a functions with VC++ or did I miss something? My thoughts are replacing lambdas with functors but still I have not idea how to implement it (i'm not a proffesional programmer)
I have no Clang/llvm but I belive this code should be compiled by Clang of version >=4. Right?